对同一个网址执行多个并行cURL?

时间:2018-07-19 07:11:28

标签: php curl soap parallel-processing php-curl

我需要使用cURL并行执行2个请求,以从Web服务获取回复。

问题是我需要从第一个XML的输出中获取加密的密码,并将其传递给第二个XML,以从API获得100个成功响应。

当前,我创建了2个cURL来实现此目的,但是API响应“ 101 Password Expired”,因为加密的密码仅对第一个请求有效。

这是我的参考代码:

第一个网址:

$soapUrl = "http://localhost:54934/frmMutualFund.asmx?op=getPassword"; // asmx URL of WSDL

// xml post structure

$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>
                    <getPassword xmlns="http://tempuri.org/">
                      <pUserId>12345</pUserId>
                      <pPassword>98765</pPassword>
                      <pPasskey>ksjhdfksj</pPasskey>
                    </getPassword>
                  </soap:Body>
                </soap:Envelope>';   // data from the form, e.g. some ID number

   $headers = array(
                "Content-type: text/xml;charset=\"utf-8\"",
                "Accept: text/xml",
                "Cache-Control: no-cache",
                "Pragma: no-cache",
                "SOAPAction: http://tempuri.org/getPassword", 
                "Content-length: ".strlen($xml_post_string),
            ); //SOAPAction: your op URL

    $url = $soapUrl;

    // PHP cURL  for https connection with auth
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //curl_setopt($ch, CURLOPT_USERPWD, $soapUser.":".$soapPassword); // username and password - declared at the top of the doc
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

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

第二个cURL:

$soapUrl = "http://localhost:54934/frmMutualFund.asmx"; // asmx URL of WSDL

// xml post structure

$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>
                        <MFUAPI xmlns="http://tempuri.org/">
                          <pFlag>06</pFlag>
                          <pUserId>12345</pUserId>
                          <pEncPassword>'.$response.'</pEncPassword>
                         <pParam>some_parameters</pParam>
                        </MFUAPI>
                      </soap:Body>
                    </soap:Envelope>';   // data from the form, e.g. some ID number

   $headers = array(
                "Content-type: text/xml;charset=\"utf-8\"",
                "Accept: text/xml",
                "Cache-Control: no-cache",
                "Pragma: no-cache",
                "SOAPAction: http://tempuri.org/MFUAPI", 
                "Content-length: ".strlen($xml_post_string),
            ); //SOAPAction: your op URL

    $url = $soapUrl;

    // PHP cURL  for https connection with auth
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //curl_setopt($ch, CURLOPT_USERPWD, $soapUser.":".$soapPassword); // username and password - declared at the top of the doc
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP 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);
    // user $parser to get your data out of XML response and to display it.
    print_r($parser);

1 个答案:

答案 0 :(得分:0)

第一

  

我需要并行执行2个请求

然后

  

问题是我需要从   第一个XML的输出并将其传递给第二个XML获得100成功   来自API的响应。

如果第二个请求的请求主体取决于第一个请求的响应,则您不能并行执行这两个请求。

(通常来说。您可以在发送第一个请求,然后部分发送第二个请求的地方进行一些微优化,然后等待第一个请求的密码被检索,然后完成第二个请求,但是很难实现,收益可能会很小,如果您输入错了时间,则可能会导致第二个请求超时,并且不得不重新启动第二个请求,这甚至会更慢除了首先要依次执行两个请求之外,您还需要可靠的2nd request timeout'ed while waiting for the password of the 1st request恢复代码,这很容易将其编码错误。)