Any folks out there tried accessing Twitter via curl_multi in PHP? My
app is setup so that, when a user makes a requests, I ask Twitter for
the friends timeline, the reply timeline, the direct_message timeline
and the direct_message/sent timeline. Since I deployed the code, I've
been having problem where my requests have been failing - according to
curl_getInfo, they're failing with HTTP status code 0, which I'm
assuming means that curl's not even getting any data.
I build up the multi requests like this ($targets is an array
containing the URL path for each requests, along with some other data):
$mh = curl_multi_init();
foreach ($targets as $each_target) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "www.twitter.com" .
$each_target['path']);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 180);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$chs[] = $ch;
curl_multi_add_handle($mh, $ch);
}
I then run through the responses like this
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
if (CURLM_OK != $mrc) {
break;
}
while ($done = curl_multi_info_read($mh)) {
$handle_index = array_search($done['handle'], $chs);
if (false !== $handle_index) {
$response = curl_multi_getcontent($done['handle']);
$response_code = curl_getinfo($done['handle'],
CURLINFO_HTTP_CODE);
...
}
}
For calls that seem like they should otherwise be working, I'm getting
back connection failed, HTTP status 0.
Sometimes, the curl error is reported as timeout, which makes no sense
because it was happening almost immediately (I previously had a 3
minute timeout set and was seeing this issue - I reverted back to the
default curl timeout as an experiment). Other times, curl reports that
it "Couldn't connect to host"
Thanks,
--Eric