并行卷曲的并发卷曲:将结果返回给调用者

时间:2015-05-07 15:19:13

标签: php web-services curl

我正在尝试使用parallel curl library进行并发卷曲调用。到目前为止,一切正常。但是,我在将结果返回给原始调用者时遇到问题。 这是我的代码:

function run_multi_curl($opts)
{
    require_once('/lib/parallelcurl/parallelcurl.php');
    $parallel_curl = new ParallelCurl($max_requests = 10, array());
    $result        = array(); //holds the results of completed requests

    // This function gets called back for each request that completes
    function on_request_done($content, $url, $ch, $flight_id)
    {
        global $result; //so we can get access to the $result variable of the enclosing method

        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 

        if ($httpcode !== 200) 
        {
            $result[] = array("flight_id"=>$flight_id, "data"=>"Fetch error $httpcode for '$url'\n");
        }
        else
        {
            $result[] = array("flight_id"=>$flight_id, "data"=>$content);
        }
    }

    foreach($opts AS $opt_array)
    {
        $xml_str  = '<?xml version="1.0" encoding="utf-8"?>'.
                    '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'.
                     '<soap:Body>'.
                      '<msg xmlns="http://videcom.com/">'.
                       '<Token>secret</Token>'.
                       '<Command>'. $opt_array['query_data']. '</Command>'.
                      '</msg>'.
                     '</soap:Body>'.
                    '</soap:Envelope>';  

        $headers = array
        ( 
            'Content-Type: text/xml; charset="utf-8"', 
            'Content-Length: '. strlen($xml_str), 
            'Accept: text/xml', 
            'Cache-Control: no-cache', 
            'Pragma: no-cache', 
            'SOAPAction: "soap action goes here"'
        );

        $parallel_curl->setOptions(array(CURLOPT_SSL_VERIFYPEER=>false,CURLOPT_SSL_VERIFYHOST=>false,CURLOPT_HTTPHEADER=>$headers,CURLOPT_HTTPAUTH=>CURLAUTH_BASIC));

        $parallel_curl->startRequest(webserviceurl.com?wsdl, 'on_request_done', $opt_array['flight_id'], $xml_str);
    }

    $parallel_curl->finishAllRequests();

    //test repeatedly for when the last call has been processed
    while(true)
    {
        if(count($result) == count($opts))
        {
            break;
        }

        usleep(10000);
    }

    return $result; //return array of combined results to calling function
}

我要做的是确定何时处理了每个调用,然后将组合结果返回给调用者。我认为使用while调用的usleep循环会执行此操作。但它不起作用。任何正确方向的指针都将受到赞赏。

0 个答案:

没有答案