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
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);
}
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
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With