PHP interesse |
|
Ik heb een vraagje, door middel van dit kan ik in de terminal de headers ophalen van een site:
curl -I -H 'Accept-Encoding: gzip,deflate' http://www.example.com
curl -I -H 'Accept-Encoding: gzip,deflate' http://www.example.com
Hoe kan ik dit doen in PHP met de cURL library? (http://www.php.net/manual/en/book.curl.php bedoel ik)
Jeroen
PS Wat ik al had geprobeerd was het volgende, maar daar kreeg ik hele andere headers dan bij de regel code hierboven:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $_GET ['url']);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$header = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);
$curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $_GET ['url']); curl_setopt($curl, CURLOPT_HEADER, true); curl_setopt($curl, CURLOPT_NOBODY, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $header = curl_exec($curl); $info = curl_getinfo($curl); curl_close($curl);
[EDIT] Ben er al uitrgekomen
Voor als mensen het willen weten:
/**
* Get response headers of a HTTP request
*
* @param string $url
* @return array;
*/
function get_headers_curl ( $url ) {
// Initialize cURL
$ch = curl_init();
// Set some options
curl_setopt ( $ch, CURLOPT_URL, $url);
curl_setopt ( $ch, CURLOPT_HEADER, true);
curl_setopt ( $ch, CURLOPT_NOBODY, true);
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ( $ch, CURLOPT_PORT, '80' );
curl_setopt ( $ch, CURLOPT_ENCODING, 'gzip,deflate' );
curl_setopt ( $ch, CURLOPT_TIMEOUT, 10);
// Set user agent
$agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.99 Safari/533.4';
curl_setopt ( $ch, CURLOPT_USERAGENT, $agent);
// Perform HTTP request
$response = curl_exec($ch);
// Split headers
$headers = explode( "\n", $response );
// Pop last 2 values, always empty
array_pop ( $headers );
array_pop ( $headers );
// Return
return $headers;
}
/** * Get response headers of a HTTP request * * @param string $url * @return array; */ function get_headers_curl ( $url ) { // Initialize cURL $ch = curl_init(); // Set some options curl_setopt ( $ch, CURLOPT_URL, $url); curl_setopt ( $ch, CURLOPT_HEADER, true); curl_setopt ( $ch, CURLOPT_NOBODY, true); curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true); curl_setopt ( $ch, CURLOPT_PORT, '80' ); curl_setopt ( $ch, CURLOPT_ENCODING, 'gzip,deflate' ); curl_setopt ( $ch, CURLOPT_TIMEOUT, 10); // Set user agent $agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.99 Safari/533.4'; curl_setopt ( $ch, CURLOPT_USERAGENT, $agent); // Perform HTTP request $response = curl_exec($ch); // Split headers $headers = explode( "\n", $response ); // Pop last 2 values, always empty // Return return $headers; }
|