叫肥皂api

时间:2016-12-16 11:45:11

标签: php soap

我是Soap Api的新手,我不了解SOAP请求。我对如何使用php调用api没有任何理想。下面是api xlm参数。

<xsd:user_id>admin@uss.com</xsd:user_id>
<xsd:password>uss</xsd:password>
<xsd:shipment>
    <xsd1:courier>L</xsd1:courier>
    <xsd1:delivery_address_line_1>123 MAIN ST</xsd1:delivery_address_line_1>
</xsd:shipment>

我只是尝试使用下面的代码

来调用此api
       $xml_post_string = '';
       $soapUrl = 'https://sandbox.loomis-express.com/axis2/services/USSBusinessService?wsdl';
       $headers = array(
                    "Content-type: text/xml;charset=\"utf-8\"",
                    "Accept: text/xml",
                    "Cache-Control: no-cache",
                    "Pragma: no-cache",
                    "SOAPAction: $soapUrl", 
                    "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, 0);
        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);

提前致谢

1 个答案:

答案 0 :(得分:0)

使用Curl调用soap服务器真的很难做到这一点并需要一些经验。 首先; 您正在向给定服务器发布空变量。

$xml_post_string = '';

您必须将预期的xml放入此变量中。我在你的代码中检查了Wsdl服务。我建议您使用SoapUI来检查Soap Server期望的XML和头参数。

例如,此soap服务器上有getVersion方法。当我使用SoapUI调用此方法时,它将为您提供Request XML和Raw Response示例。

请求XML(您必须使用您的卷曲请求发布此内容):

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:ws="http://ws.business.uss.transforce.ca">
   <soap:Header/>
   <soap:Body>
      <ws:getVersion/>
   </soap:Body>
</soap:Envelope>

此请求的预期标头(并且您必须将此标头添加到您的Curl请求中):

Content-Type: application/soap+xml;charset=UTF-8;action="urn:getVersion"
Host: sandbox.loomis-express.com

回复:

    <?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"><soapenv:Body><ns:getVersionResponse xmlns:ns="http://ws.business.uss.transforce.ca"><ns:return xmlns:ax27="http://dto.uss.transforce.ca/xsd" xmlns:ax25="http://ws.business.uss.transforce.ca/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax25:GetVersionRs"><ax25:error xsi:nil="true" /><ax25:version>3.1.8</ax25:version></ns:return></ns:getVersionResponse></soapenv:Body></soapenv:Envelope>

我建议你使用PHP的soap客户端。这是一个很好的主题:How to make a PHP SOAP call using the SoapClient class