2

我正在使用 PHP curl 对 Web 服务执行 SOAP 请求,我有一个包含 500 多个项目的 ID 数组,对于每个 ID,我发送一个 SOAP 请求并获得一个 XML 响应,我测试一些参数并生成/存储在一个变量中. 不幸的是,我只能循环遍历数组的 9 个 ID,在 jquery 帖子上的 firebug 中我得到“500 服务器错误”。如何循环浏览所有这 500 个项目?可以通过并行连接或多线程来完成吗?如果是这样,你如何在 PHP curl 中使用它?

这是我的示例代码。

<?php 
$VehicleIDs = "153,106,128,149,121,123,125,133,130,115,124,116,102,100,101,103,144,113,...........";//over 500 items
$VehicleIDsArray = explode(",", $VehicleIDs);

for($i=0; $i <= count($massVehicleIDsArray); $i++){
            $soapUrl = "http://xxx.asmx";
            $xml_post_string = '<?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>
                                <GetInUseEventsForVehicleID>
                                  <VehicleID>'.$massVehicleIDsArray[$i].'</VehicleID>
                                </GetInUseEventsForVehicleID>
                              </soap:Body>
                            </soap:Envelope>';

        $headers = array(
                    "Content-type: text/xml;charset=\"utf-8\"",
                    "Accept: text/xml",
                    "Cache-Control: no-cache",
                    "Pragma: no-cache",
                    "SOAPAction: xxx", // your op URL
                    "Content-length: ".strlen($xml_post_string),
                );
        // PHP cURL  for https connection with auth
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_URL, $soapUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // xml request
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        // converting
        $response = curl_exec($ch); 
        curl_close($ch);

        // converting
        $response1 = str_replace("<soap:Body>","",$response);
        $response2 = str_replace("</soap:Body>","",$response1);

        // convertingc to XML
        $parser = simplexml_load_string($response2);   

        /*....................

        MORE CODE TO COMPARE EACH SOAP RESPONSE

        ...................*/
}
?>

谢谢

4

2 回答 2

1

我认为基本问题是在低时间限制http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time,是的 curl 支持并行执行http://php.net /manual/ru/function.curl-multi-exec.php,有一些用于并行 curl 执行的开源库。

http://www.phpclasses.org/package/4091-PHP-Retrieve-multiple-pages-simultaneously.html

https://github.com/Fivell/Utfy

https://github.com/jmathai/php-multi-curl

等等。但是您仍然可能会达到最大执行时间或内存限制

于 2012-11-27T09:15:58.410 回答
0

另一种可能性是您在同一页面中多次调用 php 中的 wsdl 方法

在客户端连接之前创建,然后执行类似的操作

$params1 = array(xml nodes);
$client->WSDLMETHOD($params1);

echo $client->__getLastRequest();
echo $result = $client->__getLastResponse(); 

//reapeat this and you will do two soap request
$params2 = array(xml nodes);
$client->WSDLMETHOD($params2);
echo $client->__getLastRequest();
echo $result = $client->__getLastResponse();
于 2018-01-16T17:34:02.743 回答