PHP multi curl works on one machine, fails on another
I am trying to run a parallel image downloading with multi curl. I have a
short script that works fine for me on my laptop (php 5.3.10-1) but fails
on my server (PHP 5.5.3-1).
When I run this script on my server the target files are being created (I
guess that curl opens the handle successfully) but the files are empty,
there is no warning or error that I could see on my server to indicate any
issue.
Following is a sample of the original POC code I wrote/found online and
runs perfectly on my laptop but fails as is on my server. I would
appreciate your thoughts. Thank you.
<?php
$urls = array(
'http://static.php.net/www.php.net/images/php.gif',
'http://p.ebaystatic.com/aw/pics/globalheader/spr9.png'
);
$save_to = '/tmp/';
function add_file_to_curl($save_to, $mh, $url, $i){
global $conn, $fp;
$g=$save_to.basename($url);
if(is_file($g)){
unlink($g);
}
$conn[$i]=curl_init($url);
$fp[$i]=fopen ($g, "w");
curl_setopt ($conn[$i], CURLOPT_FILE, $fp[$i]);
curl_setopt ($conn[$i], CURLOPT_HEADER ,0);
curl_setopt($conn[$i],CURLOPT_CONNECTTIMEOUT,60);
curl_multi_add_handle ($mh,$conn[$i]);
}
$conn="";
$fp="";
$mh = curl_multi_init();
foreach ($urls as $i => $url) {
add_file_to_curl($save_to, $mh, $url, $i);
echo "URL IS $url, I is $i\n";
}
do {
$n = curl_multi_exec($mh,$running);
$ready = curl_multi_select($mh); // Waiting for one of the
files to finish
if(0 < $ready){
while($info = curl_multi_info_read($mh)){
$status =
curl_getinfo($info['handle'],CURLINFO_HTTP_CODE);
echo "STATUS $status\n";
if(200 == $status){
$successUrl =
curl_getinfo($info['handle'],CURLINFO_EFFECTIVE_URL);
echo "$successUrl\n";
}
break 1;
}
}
}
while (0 < $running && -1 != $ready);
$info = curl_multi_info_read($mh);
$status = curl_getinfo($info['handle'],CURLINFO_HTTP_CODE);
echo "STATUS $status\n";
if(200 == $status){
$successUrl =
curl_getinfo($info['handle'],CURLINFO_EFFECTIVE_URL);
echo "$successUrl\n";
}
foreach ($urls as $i => $url) {
echo "Running on $url and $i\n";
curl_multi_remove_handle($mh,$conn[$i]);
curl_close($conn[$i]);
fclose ($fp[$i]);
}
curl_multi_close($mh);
?>
No comments:
Post a Comment