I have been following this tutorial on how to use curl_multi
. http://arguments.callee.info/2010/02/21/multiple-curl-requests-with-php/
I can't tell what I am doing wrong, but curl_multi_getcontent
is returning null. It is suppose to return JSON. I know it is not the mysql call as I had it working with a while loop and standard curl_exec
, but The page was taking too long to load. (I've changed some of the setopt details for security)
Relevant PHP Code snippet. I do close the while loop in the end.
$i = 0;
$ch = array();
$mh = curl_multi_init();
while($row = $result->fetch_object()){
$ch[$i] = curl_init();
curl_setopt($ch[$i], CURLOPT_CAINFO, 'cacert.pem');
curl_setopt($ch[$i], CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch[$i], CURLOPT_URL, 'https://mysite.com/search/'.$row->username.'/');
curl_multi_add_handle($mh, $ch[$i]);
$i++;
}
$running = 0;
do {
curl_multi_exec($mh, $running);
} while ($running > 0);
$result->data_seek(0);
$i = 0;
while ($row = $result->fetch_object()) {
$data = curl_multi_getcontent($ch[$i]);
$json_data = json_decode($data);
var_dump($json_data);
EDIT
Here is the code that currently works, but causes the page to load too slowly
$ch = curl_init();
curl_setopt($ch, CURLOPT_CAINFO, 'cacert.pem');
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
while($row = $result->fetch_object()){
curl_setopt($ch, CURLOPT_URL, 'https://mysite.com/search/'.$row->username.'/');
$data = curl_exec($ch);
$json_data = json_decode($data);
var_dump($json_data);
}
$ch[$i]
too see if that's containing what it should? – Tryparsamide$ch[$i]
I getresource(#) of type (curl)
– Brightnessvar_dump(curl_errno($ch[$i]));
inside the second while to see what errors you get. – Tertiaryvar_dump(curl_error($ch[$i]));
in conjunction withcurl_errno
– Tertiarycurl_errno
returnedint(0)
whilecurl_error
returnedstring(60) "Unknown SSL protocol error in connection to mysite.com:443 "
I am defining the cacert.pem file so I don't understand why I am getting this error. – Brightnesscurl_setopt($ch[$i], CURLOPT_SSL_VERIFYPEER, false);
fixes the issue and my page loads fine. Cancurl_multi_exec
not accept thecurlopt_cainfo
option? – Brightnesscurl_multi_init
but those were generally memory leaks. I don't think theCURLOPT_CAINFO
option is the problem but the inability of the curl lib to negotiate a SSL protocol. It might help to assign the ciphers usingCURLOPT_SSL_CIPHER_LIST
but it's no guarantee. Also, double check the URL sent inCURLOPT_URL
as a simple typo there could result in this error if your DNS provider redirects failed DNS requests to another host. – Tertiary