Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP multi curl works on one machine, fails on another

Tags:

php

curl

debian

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.

I am testing it via CLI at the moment, so the php.ini for php's cli has:

error_reporting = E_ALL
display_errors = On 

That's the first thing I did. The only thing I see is a warning: PHP Warning: Module 'PDO' already loaded in Unknown on line 0

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. Thanks and sorry for not being clear regarding the debug level.

    <?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);
    ?>
like image 762
GibsonLP Avatar asked Oct 22 '22 01:10

GibsonLP


1 Answers

It appears that something is wrong with the php on my server. Even the sample from http://php.net/manual/en/function.curl-multi-exec.php doesn't work for me (it just hangs there, strace shows that it simply hangs on clone(child_stack=.... ) I'll fix it but it has nothing to do with my code so I decided to "close" my case.

And I would like to take this opportunity to personally thank the person who was nice enough to -1 my question despite having no clue what's wrong. Very helpful.

like image 111
GibsonLP Avatar answered Oct 24 '22 11:10

GibsonLP