// the best fuction so you stop saving time.
function curlwiz($uri, $method='GET', $data=null, $curl_headers=array(), $curl_options=array()) {
// default curl options which will almost be static, you can modify if you want
$default_curl_options = array(
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 3,
);
// you can set the default headers into this array, usually you dont need them.
$default_headers = array();
// We need to trim and change into MAYUS the method passed
$method = strtoupper(trim($method));
$allowed_methods = array('GET', 'POST', 'PUT', 'DELETE'); // array with allowed methods.
if(!in_array($method, $allowed_methods)) // if the method from input is not in allowed_methods array, then throw an error.
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 from curl
$raw = rtrim(curl_exec($curl));
//var_dump($raw);
$lines = explode("\r\n", $raw); // we exploder curl response line by line
var_dump($lines);
$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
);
}
curlwiz('http://facebook.com', 'GET');