Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP curl exec fail on php script same domain

Tags:

php

curl

I use php curl to get content from a php script in the same domain url. But I get curl_exec error. The curl error code is 28 or operation timed out. After days of debugging, I found that it works on non script page like htm, but not php, it also works if the url is a script on different domain. I have been debugging for days and found no solutions. Helps appreciated.

$url = 'http://...';
$agent = '';
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 8);
curl_setopt($ch, CURLOPT_TIMEOUT, 8);
$result = curl_exec ($ch);
print "<pre>\n";
print_r(curl_getinfo($ch));
// get error info echo "\n\ncURL error number:" .curl_errno($ch);
// print error info echo "\n\ncURL error:" . curl_error($ch);
print "</pre>\n";
curl_close ($ch);
echo $result;

cURL error number:28 cURL error:

Operation timed out after 8000 milliseconds with 0 bytes received

Okay: $url = http://.../page.htm Fail: $url = http://.../page.php

like image 852
Iceberg Avatar asked Feb 26 '11 17:02

Iceberg


3 Answers

You're not setting the User Agent. Some servers actually do not even respond to such requests (thus the client doesn't know the connection dropped).

Add the following to your code:

curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);

Additionally, I use the following in my CURL functionality:

curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch,CURLOPT_MAXREDIRS,50);
if(substr($url,0,8)=='https://'){
    // The following ensures SSL always works. A little detail:
    // SSL does two things at once:
    //  1. it encrypts communication
    //  2. it ensures the target party is who it claims to be.
    // In short, if the following code is allowed, CURL won't check if the 
    // certificate is known and valid, however, it still encrypts communication.
    curl_setopt($ch,CURLOPT_HTTPAUTH,CURLAUTH_ANY);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
}
like image 123
Christian Avatar answered Oct 05 '22 07:10

Christian


Maybe because of session locks
Try to drop session_start() from page which you try to get using cURL

Also see this session.auto-start

like image 35
azat Avatar answered Oct 05 '22 08:10

azat


Set into CURL setting

curl_setopt($ch, CURLOPT_TIMEOUT, 300); // 300 seconds

If still have issue so run the script with following location disable.

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
like image 31
Jaydip Kansagra Avatar answered Oct 05 '22 08:10

Jaydip Kansagra