I ran into this issue trying to connect to an API multiple times in succession.
My answer is pretty much the same as @developer:
Possibly you call too many connections from your one Ip Address, So the server blocks the connection and causes the on/off errors.
This works for me as a work around.
Create a method that attempts the curl request a few times, adding time between each request until it gets data or gives up.
/**
* Thanks to issues with sometimes not receiving data when making multiple requests
* here we make a reasonable effort to get data even when a firewall or WAF or ?? maybe
* throttling request, DDOS, or on fire
*
* @param Curl $ch
*
* @return bool|string
*/
function curlExecFireWalker($ch)
{
//In case we fail, let's count our failures
$wowFailCount = 0;
do {
$data = curl_exec($ch);
if (!$data) {
//Well this is awkward, we are expecting data and didn't get any :(
$wowFailCount++;
//Let's calculate the length of a smoke break
$smokeBreak = pow($wowFailCount, 2);
//Smoke 'em if you got 'em
sleep($smokeBreak);
}
//Well let's not allow to many failures and get out of here before the entire thing is on fire
} while (!$data && $wowFailCount <= 3);
return $data;
}
Then instead of
$data = curl_exec($ch);
call the fire walker method
$data = curlExecFireWalker($ch);
curl_error($ch);
php.net/curl_error – Concernexit;
afterecho $returned_content
; – Katelynnkaterinawww.example.com
is not a URL, you are missing the protocol. – Austrasia