Using cURL with PHP cannot get headers for fewer sites

I'm having trouble using curl to get headers for fewer sites.

Some examples are digg.com and microsoft.com.

function get_headers_curl($url, $port)
{
    $ch = curl_init();

    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,           $port);
    curl_setopt($ch, CURLOPT_TIMEOUT,        10);

    $r = curl_exec($ch);
    $r = split("\n", $r);
    return $r;
}

      

This is the function and parameters I am currently using and for ease of use I have a small test script running @ isitup.org/test.php?d=example.com... It just returns the response headers, and with sample sites it is missing one.

The problem is that these sites seem to be ignoring the request and I am not getting a response. I had a game with different options, but I can't get an answer.

Is there something I am missing? Or is it just not possible to access such sites using curl?

Hello,

Sam

Edit:

test.php:

<?php

$domain = preg_replace("/[^A-Za-z0-9-\/\.\:]/", "", trim($_GET["d"]));

$agent = "Mozilla/5.0 (X11; U; Linux i686; pl-PL; rv:1.9.0.2) Gecko/20121223 Ubuntu/9.25 (jaunty) Firefox/3.8";

function get_headers_curl($url, $port)
{
    $ch = curl_init();

    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,           $port);
    curl_setopt($ch, CURLOPT_TIMEOUT,        10);
    curl_setopt($ch, CURLOPT_USERAGENT,      $agent);


    $r = curl_exec($ch);
    $r = split("\n", $r);
    return $r;
}

$headers = get_headers_curl("http://".$domain, 80);

print("<pre>".print_r($headers,true)."</pre>");


?>

      

However, the new user agent is still not receiving a response from these sites ...

Update: Woops saw my error pushed agent into function and yes it works! Thanks: P

+2


source to share


1 answer


Both work fine for me when I add the user agent line with CURLOPT_USERAGENT.



// e.g.
$agent = 'Mozilla/5.0 (X11; U; Linux i686; pl-PL; rv:1.9.0.2) Gecko/20121223 Ubuntu/9.25 (jaunty) Firefox/3.8';
curl_setopt($ch, CURLOPT_USERAGENT, $agent);

      

+4


source







All Articles