1

Hello StackOverflow community, I've encountered a problem when I try to use cURL methods on PHP. I tried with this sample code:

$html_brand = "www.google.com/";
$ch = curl_init();

$options = array(
    CURLOPT_URL            => $html_brand,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HEADER         => true,
    CURLOPT_HTTPHEADER     => array("User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"),
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => false,
    CURLOPT_ENCODING       => "",
    CURLOPT_AUTOREFERER    => true,
    CURLOPT_CONNECTTIMEOUT => 120,
    CURLOPT_TIMEOUT        => 120,
    CURLOPT_MAXREDIRS      => 10,
);
curl_setopt_array( $ch, $options );
$response = curl_exec($ch); 
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ( $httpCode != 200 ){
    echo "Return code is {$httpCode} \n"
        .curl_error($ch);
} else {
    echo "<pre>".htmlspecialchars($response)."</pre>";
}

curl_close($ch);

And it always ends with an error, displaying it on screen:

Return code is 0 Recv failure: Connection was reset

Or this error, when trying to reach any site with https:

Return code is 0 Failed to connect to www.google.com port 443: Timed out

This are my settings:

  • Windows 7 Professional 32 bit
  • Apache 2.4.12
  • PHP 5.6.11

Is it a code error or any server configurations I have not considered?

The HTTP_HOST value in Apache is localhost:8080, which I'm not really sure if it has anything to do with my problem, but maybe it's worth noting.

Thank all of you in advance.

5
  • Have you checked your firewall logs to see if the request made it that far? Commented Oct 12, 2015 at 16:28
  • is this question, then I can't figure out what is difference b/w answer and question, look stackoverflow.com/a/10228476/2558525 Commented Oct 12, 2015 at 17:02
  • Possible duplicate of PHP cURL HTTP CODE return 0 Commented Oct 12, 2015 at 17:18
  • @RowlandShaw, firewalls are deactivated on my Windows system Commented Oct 12, 2015 at 17:56
  • @MubinKhalid, I took my sample code from that answer to test if it could solve my problem as well, obviously it didn't. I added a few lines like CURLOPT_HTTPHEADER, but they did not make any difference. I used it as a sample in this question because it seems to be the most coherent, complete and simple test code that I have found in the last few hours, and it would be nice to make it work for further coding on my own. Commented Oct 12, 2015 at 18:04

1 Answer 1

0

I've developed a custom function, that works fine for GET, POST and ajax requests.

So here it's

    function spider($header = array(), $referer = false, $url, $cookie = false,$post = false)
    {
        if (!$cookie)
        {
            $cookie = "cookie.txt";
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate,sdch');
        if (isset($header) && !empty($header))
        {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 200);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
        curl_setopt($ch, CURLOPT_USERAGENT,
            "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7");
        curl_setopt($ch, CURLOPT_COOKIEJAR, realpath($cookie));
        curl_setopt($ch, CURLOPT_COOKIEFILE, realpath($cookie));
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        if (isset($referer) && $referer != false)
        {
            curl_setopt($ch, CURLOPT_REFERER, $referer);
        } else
        {
            curl_setopt($ch, CURLOPT_REFERER, $url);
        }
        //if have to post data on the server
        if (isset($post) && !empty($post) && $post)
        {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        } //endif
        $data = curl_exec($ch);
        $info = curl_getinfo($ch);
        print_r($info);
        curl_close($ch);
        if($info['http_code'] == 200){
            return ($data);
        }else{
            return false;
        }
    }

so, parameters are,

$header => Headers to send to server, this should be associative array.

$referer => Referrer to current page(if any).

$url => URL that you want to get.

$cookie => cookie file, should be on the same level where the calling script is.

$post => data to post (if any)

I tested this function as follows.

echo spider(FALSE, FALSE, 'https://www.google.com/ncr');

here is header that I got.

Array
(
[url] => https://www.google.com/
[content_type] => text/html; charset=UTF-8
[http_code] => 200
[header_size] => 1665
[request_size] => 701
[filetime] => -1
[ssl_verify_result] => 20
[redirect_count] => 1
[total_time] => 2.215
[namelookup_time] => 0
[connect_time] => 0
[pretransfer_time] => 0
[size_upload] => 0
[size_download] => 9024
[speed_download] => 4074
[speed_upload] => 0
[download_content_length] => 0
[upload_content_length] => 0
[starttransfer_time] => 0.437
[redirect_time] => 1.7
[certinfo] => Array
    (
    )

[redirect_url] => 
)

and here is snapshot of this request.

enter image description here

Hope this will help you...

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the quick reply, unfortunately it did not solve my problem, I tested your function the same way you did and I got an array with most values on 0: Array ( [url] => https://www.google.com/ncr [content_type] => [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 20.218 [namelookup_time] => 0.015..., total_time and namelookup_time are the only elements with other value than 0. I don't really think that my problem is code-related now but I may keep your function as testing code for the future :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.