配置soap api xml

时间:2016-09-28 10:42:15

标签: php xml web-services api soap

我想用php配置一个api of soap,下面的xml用于api,但是没有响应,请帮我恢复这个问题

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fram="http://framework.zend.com">
<soapenv:Header/>
<soapenv:Body>
<fram:getCountries>
<securityInfo>
<userName>username</userName>
<password>password</password>
<agentCode>code</agentCode>
<lang>en</lang>
</securityInfo>
</fram:getCountries>
</soapenv:Body>
</soapenv:Envelope>

下面显示我用来调用xml的php代码。但我没有得到我的PHP代码的任何响应,但xml是正确的,我已经向提供商询问他们告诉我xml是正确的但我的PHP代码不正确。我不是我要做的就是解决这个问题:(

 <?php 
$soapUrl = "http://testapi.roombookpro.com/en/soap/index?wsdl"; // asmx URL of WSDL
$soapUser = "username";  //  username
$soapPassword = "password"; // password

// xml post structure
$xml_post_string = '<?xml version="1.0" encoding="utf-8"?>
  <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fram="http://framework.zend.com">
  <soapenv:Header/>
  <soapenv:Body>
  <fram:getCountries>
  <securityInfo>
  <!-- You may enter the following 4 items in any order  -->
  <userName>username</userName>
  <password>password</password>
  <agentCode>agentcode</agentCode>
  <lang>en</lang>
  </securityInfo>
  </fram:getCountries>
  </soapenv:Body>
  </soapenv: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://testapi.roombookpro.com/en/soap/index#getCountries",
  "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);
// converting
$response1 = str_replace("<soap:Body>","",$response);
$response2 = str_replace("</soap:Body>","",$response1);
// convertingc to XML
$parser = simplexml_load_string($response2);
var_dump($parser);
echo $parser;
// user $parser to get your data out of XML response and to display it.
  ?>

1 个答案:

答案 0 :(得分:0)

在您的代码中,您的请求指向下面的wsdl链接

http://testapi.roombookpro.com/en/soap/index?wsdl

这就是为什么你没有得到肥皂反应而不是得到定义。

你应该使用以下肥皂网址来获取肥皂回复,

http://testapi.roombookpro.com/en/soap/index

修改 并获取XML响应(作为字符串):

$response = curl_exec($ch); 
curl_close($ch);
print_r($response);

获取SimpleXMLElement对象:

      $xml = simplexml_load_string($response);
        foreach ($xml->xpath('//ns2:Soap_Model_SOAP_Location_Country') as $item)
        {
            print_r($item);
 //to access individual elements 
//        echo $item->id ;
//        echo $item ->code;
//        echo $item->name;
//        echo "\n";

        }

提供XML对象数组:

SimpleXMLElement Object
(
    [id] => 252
    [code] => ZW
    [name] => Zimbabwe
)