Skip to content

Instantly share code, notes, and snippets.

@ViliamKopecky
Created May 10, 2012 00:57
Show Gist options
  • Save ViliamKopecky/2650186 to your computer and use it in GitHub Desktop.
Save ViliamKopecky/2650186 to your computer and use it in GitHub Desktop.
Simple cURL wrapper function for GET, POST, PUT & DELETE methods
/**
* Wrapper for easy cURLing
*
* @author Viliam Kopecký
*
* @param string HTTP method (GET|POST|PUT|DELETE)
* @param string URI
* @param mixed content for POST and PUT methods
* @param array headers
* @param array curl options
* @return array of 'headers', 'content', 'error'
*/
function simple_curl($uri, $method='GET', $data=null, $curl_headers=array(), $curl_options=array()) {
// defaults
$default_curl_options = array(
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 3,
);
$default_headers = array();
// validate input
$method = Strings::upper(trim($method));
$allowed_methods = array('GET', 'POST', 'PUT', 'DELETE');
if(!in_array($method, $allowed_methods))
throw new \Exception("'$method' is not valid cURL HTTP method.");
if(!empty($data) && !is_string($data))
throw new \Exception("Invalid data for cURL request '$method $uri'");
// init
$curl = curl_init($uri);
// apply default options
curl_setopt_array($curl, $default_curl_options);
// apply method specific options
switch($method) {
case 'GET':
break;
case 'POST':
if(!is_string($data))
throw new \Exception("Invalid data for cURL request '$method $uri'");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case 'PUT':
if(!is_string($data))
throw new \Exception("Invalid data for cURL request '$method $uri'");
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case 'DELETE':
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
break;
}
// apply user options
curl_setopt_array($curl, $curl_options);
// add headers
curl_setopt($curl, CURLOPT_HTTPHEADER, array_merge($default_headers, $curl_headers));
// parse result
$raw = rtrim(curl_exec($curl));
$lines = explode("\r\n", $raw);
$headers = array();
$content = '';
$write_content = false;
if(count($lines) > 3) {
foreach($lines as $h) {
if($h == '')
$write_content = true;
else {
if($write_content)
$content .= $h."\n";
else
$headers[] = $h;
}
}
}
$error = curl_error($curl);
curl_close($curl);
// return
return array(
'raw' => $raw,
'headers' => $headers,
'content' => $content,
'error' => $error
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment