using custom request headers array with curl
<p>this function passes in an array with custom request headers to curl, good if you need to pass in some headers to request an api url and give it an auth key or something like that. Also added to the array is one that disables caching. </p>
<p>this will return the data or false</p>
<h2>usage</h2>
<p><b>$url</b> : should be the api request url. </p>
<p><b>$requestheaders</b> : add items to this array to be passed as the headers.</p>
<p><b>$apikey</b> : if you are passing an api key you can specify this otherwise delete the $apikey variable.</p>
<h2>code</h2>
<pre>$requestheaders = array();
$requestheaders[] = "Authorization: Bearer " . $apikey;
$requestheaders[] = "Cache-Control: no-cache"; // disable curl cache
function getcurlhead($url, $requestheaders = "") {
$ch = curlinit();
$timeout = 5;
curlsetopt($ch, CURLOPTURL, $url);
curlsetopt($ch, CURLOPTRETURNTRANSFER, 1);
curlsetopt($ch, CURLOPTCONNECTTIMEOUT, $timeout);
curlsetopt($ch, CURLOPTHTTPHEADER, $requestheaders);
curlsetopt($ch, CURLOPTFRESHCONNECT, 1); // disable cache
$data = curlexec($ch);
curl_close($ch);
if(isset($data)) {
return $data;
} else {
return false;
}
}</pre>