带有post方法的PHP SOAP请求

时间:2015-10-28 17:21:36

标签: php xml soap

我这里有一个代码,只能获得当前的油价,但我需要得到与下图相同的结果。我不知道这样做。

The result

<?php
$soapUrl = "http://www.pttplc.com/webservice/pttinfo.asmx?op=CurrentOilPrice";

$xml_post_string = '<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <CurrentOilPrice xmlns="http://www.pttplc.com/ptt_webservice/">
      <Language>string</Language>
    </CurrentOilPrice>
  </soap12:Body>
</soap12:Envelope>';

$headers = array(
    "POST /webservice/pttinfo.asmx HTTP/1.1",
    "Host: www.pttplc.com",
    "Content-Type: application/soap+xml; charset=utf-8",
    "Content-Length: " . strlen($xml_post_string)
);

$url = $soapUrl;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

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

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

$parser = simplexml_load_string($response2);
echo $parser->asXML();

1 个答案:

答案 0 :(得分:0)

如果您使用Web服务,可以使用SOAP CLIENT是一个很好的工具。这很容易使用,可以在SimpleXMLObject中自动转换您的响应,并且您可以使用XPath来轻松查找XML标记。