PHP Curl request increase timeout - php

I have this php code that consumes an api. I have one request that takes a long time almost 4 minutes. The curl request times out and returns an empty response.
I found this solution.
ini_set('max_execution_time', 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
But this doesn't have any effect.
How do I wait for the request to finish execution.
Here is full snippet.
ini_set('max_execution_time', 0);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
curl_close($ch);

I had this error too and had to change directly the default_socket_timeout in the php.ini file. After that, you have to reload Apache and it should work.

Related

bad url never ending request curl php

I have a curl set up like this:
function file_get_contents_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 400);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch,CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$data = curl_exec($ch);
return $data;
}
When I call it with the following:
$html = file_get_contents_curl("https://training.sap.com/service-id/module.php/core/as_login.php?AuthId=sf-sp&ReturnTo=%2Fsuccessfactors-community%2Fpermission-check");
You can see that link has an authentication, and I do not want to spend time on it. I just want that curl session to die after maybe 3 seconds or x seconds, or determine before hand if it is going to be a never ending loop on the curl for one reason such a SSL or password requirement etc.

Increase curl timeout for busy servers

I've increased the timeout but it doesn't work.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost.com/test.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 999);
$response = curl_exec($ch);
print_r($response);
You need to add CURLOPT_CONNECTTIMEOUT option to it.
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 600); //10 minutes

PHP curl through proxy generating high CPU load

After investigating the reasons for the high CPU load generated by an application sending many curl requests with 20 parallel PHP-cli scripts, I've found out the main reason is the PROXY option set to the curl handle.
The following code is generating a 0.5-1.5% CPU load at the moment of curl_exec when run without the proxy and 12-25% when using any proxy (I've tried many different ones).
With many threads running many requests at the same time, it soon becomes critical.
I'm using:
Ubuntu 16.04
PHP 7.0.22
curl 7.55.1
Is there a fix or workaround I can apply?
$address = 'xxx';
$port = 'xxx';
$url = 'https://api.ipify.org?format=json';
do {
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_ENCODING, 'identity');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_PROXY, $address);
curl_setopt($ch, CURLOPT_PROXYPORT, $port);
curl_setopt($ch, CURLOPT_URL, $url);
curl_exec($ch);
curl_close($ch);
sleep(10);
} while (true);

500 Internal server error - Request Timeout CURL PHP

Hello I need some into about this error. I send loop data using curl that data take too much time to send. I set max_execution_time Limit to 900. So why this Request Timeout happen. Is that server problem or php.ini problem.
NOTE: My Proxy is Working and its high speed. I send data in loop. So after 1mint this error show. in title say 500 Internal server error and in body says Request Timeout
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT,'Opera/9.80 (Series 60; Opera Mini/6.5.27309/34.1445; U; en) Presto/2.8.119 Version/11.10');
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
echo $data = curl_exec($ch);
curl_close($ch);
You might change your curl timeout from 10 seconds (like you have it inside your code right now) from
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
to 900 seconds, like this:
curl_setopt($ch, CURLOPT_TIMEOUT, 900);
These numbers are seconds (not 100% sure). Also find according curl documentation here.

CURLOPT_CONNECTTIMEOUT is not working

i am learning about CURLOPT_CONNECTTIMEOUT, i am using a proxy in this test with 25 seconds timeout setting, but it never timed out, i dont know why. here is my simple code
$proxy="124.206.54.251:3128";
$timeout = 25;
$url = "http://google.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$headerSent = curl_getinfo($ch, CURLINFO_HEADER_OUT );
$returned_msg = curl_exec($ch);
$error = curl_error($ch);
$info= curl_getinfo($ch);
curl_close($ch);
echo "<br>".$returned_msg."<br>".$info['total_time']."<br>".$error;
can someone plz tell why it never timed out? i only want to wait 25 second and it is unable to for any reason such as proxy is dead, then it should timed out.
CURLOPT_CONNECTTIMEOUT only sets the timeout for the amount of time it takes to connect. If you want to limit the amount of time the entire request is allowed to take, use CURLOPT_TIMEOUT instead.

Categories