douhunbei0166 2015-10-28 17:21
浏览 45
已采纳

带有post方法的PHP SOAP请求

I have a code here and can get only current oil price but I need to get a result the same as the picture below. I have no idea to do this.

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条回答 默认 最新

  • doukuang6795 2015-10-30 17:48
    关注

    You can use SOAP CLIENT is a good tool for use if you work with Web services. This is easy to use, automaticaly convert your response in SimpleXMLObject and you are able to use XPath to find easy your XML tag.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?