Skip to content

Instantly share code, notes, and snippets.

@thocell
Last active May 13, 2018 11:52
Show Gist options
  • Save thocell/78ab64b8baca42fd6e671e8820018677 to your computer and use it in GitHub Desktop.
Save thocell/78ab64b8baca42fd6e671e8820018677 to your computer and use it in GitHub Desktop.
Get headers using curl--php
* In php, the function "get_headers" sometimes is slow, especially if you want to get headers of a batch of urls in parallel.
* In this case, you should use curl.
function get_headers_curl($url){
$curl = curl_init();
curl_setopt_array( $curl, array(
CURLOPT_HEADER => true,
CURLOPT_NOBODY => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $url));
$headers = explode( "\n", curl_exec($curl));
curl_close($curl);
return $headers;
}
function multiCurl_headers(array $urls){
// array of curl handles
$handles = array();
// data to be returned
$results = array();
// multi handle
$mh = curl_multi_init();
// loop through $data and create curl handles
// then add them to the multi-handle
foreach ($urls as $k => $u) {
$handles[$k] = curl_init();
curl_setopt($handles[$k], CURLOPT_URL, $u);
curl_setopt($handles[$k], CURLOPT_HEADER, true);
curl_setopt($handles[$k], CURLOPT_NOBODY, true);
curl_setopt($handles[$k], CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($mh, $handles[$k]);
}
// execute the handles
$running = null;
do {
curl_multi_exec($mh, $running);
} while ($running > 0);
// get content and remove handles
foreach ($handles as $id => $content) {
$result = curl_multi_getcontent($content);
$result = explode("\n", $result);
$results[$id] = $result;
//close the handles
curl_multi_remove_handle($mh, $content);
}
// all done
curl_multi_close($mh);
return $results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment