Curl Multi and Return Transfer

前端 未结 1 602
逝去的感伤
逝去的感伤 2021-01-25 08:56

I need to make a number of curl requests to the same domain one after the other, but cannot make them in parallel.

I found the following code sample at http://technosoph

相关标签:
1条回答
  • 2021-01-25 09:32

    Perhaps this will be of interest, very easy to understand. It will do your curl multi requests and then return an array of results, it also does curl POST.

    <?php
    //demo receiver
    if($_SERVER['REQUEST_METHOD']=='POST'){
        echo $_POST['post_var'];
        die;
    }
    
    /**
     * CURL GET|POST Multi
     */
    function curl_multi($data, $options = array()) {
        $curly = array();
        $result = array();
    
        $mh = curl_multi_init();
        foreach ($data as $id=>$d) {
            $curly[$id] = curl_init();
            $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;
    
            $header[0]="Accept: text/xml,application/xml,application/xhtml+xml,application/json";
            $header[0].="text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
            $header[]="Cache-Control: max-age=0";
            $header[]="Connection: keep-alive";
            $header[]="Keep-Alive: 2";
            $header[]="Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
            $header[]="Accept-Language: en-us,en;q=0.5";
            $header[]="Pragma: ";
            curl_setopt($curly[$id], CURLOPT_URL,            $url);
            curl_setopt($curly[$id], CURLOPT_HEADER,         0);
            curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curly[$id], CURLOPT_TIMEOUT,        30);
            curl_setopt($curly[$id], CURLOPT_USERAGENT,      "cURL (http://".$_SERVER['SERVER_NAME'].")");
            curl_setopt($curly[$id], CURLOPT_HTTPHEADER,     $header);
            curl_setopt($curly[$id], CURLOPT_REFERER,        $url);
            curl_setopt($curly[$id], CURLOPT_ENCODING,       'gzip,deflate');
            curl_setopt($curly[$id], CURLOPT_AUTOREFERER,    true);
            curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, true);
            // post?
            if (is_array($d)) {
                if (!empty($d['post'])) {
                    curl_setopt($curly[$id], CURLOPT_POST,       1);
                    curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']);
                }
            }
            // extra options?
            if (!empty($options)) {
                curl_setopt_array($curly[$id], $options);
            }
            curl_multi_add_handle($mh, $curly[$id]);
        }
        $running = null;
        do {
            curl_multi_exec($mh, $running);
        } while($running > 0);
        foreach($curly as $id => $c) {
            $result[$id] = curl_multi_getcontent($c);
            curl_multi_remove_handle($mh, $c);
        }
        curl_multi_close($mh);
        return $result;
    }
    
    $request = array(
        array('url'=>'http://localhost:8080/testing.php','post'=>array('post_var'=>'a')),
        array('url'=>'http://localhost:8080/testing.php','post'=>array('post_var'=>'b')),
        array('url'=>'http://localhost:8080/testing.php','post'=>array('post_var'=>'c')),
    );
    $curl_result = curl_multi($request);
    
    /*
    Array
    (
        [0] => a
        [1] => b
        [2] => c
    )
    */
    echo '<pre>'.print_r($curl_result, true).'</pre>';
    ?>
    
    0 讨论(0)
提交回复
热议问题