如何在 PHP 中发送 xml 数据作为请求内容

新人實習生一枚- 最近在公司項目中遇需要進行soap 獲取數據
在此留個坑
以下就是發送xml請求的兩種方法!廢話不多說直接上代碼

//歡迎大佬指正

$xmldata = <<<EOT
<?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>
    <getSupportProvince xmlns="http://WebXml.com.cn/" />
  </soap:Body>
</soap:Envelope>
EOT;
$wsdl = 'http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?WSDL';
try{
    $client = new SoapClient($wsdl);
    $result = $client->__doRequest($xmldata,$wsdl,'http://WebXml.com.cn/getSupportProvince',1,0);//发送xml請求 __doRequest
    var_dump($result);
}catch (SoapFault $e){
    echo $e->getMessage();
}catch(Exception $e){
    echo $e->getMessage();
}

2

<?php 
$soapUrl = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";
$xmldata = <<<EOT
<?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>
    <getSupportProvince xmlns="http://WebXml.com.cn/" />
  </soap:Body>
</soap:Envelope>
EOT;

$headers = array(
"POST /WebServices/WeatherWebService.asmx HTTP/1.1",
"Host: www.webxml.com.cn",
"Content-Type: text/xml; charset=utf-8",
"Content-Length: ".strlen($xmldata)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $soapUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmldata);
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);


$xmljson= json_encode($parser);
$xmlarray=json_decode($xmljson,true);
print_r($xmlarray);
本作品采用《CC 协议》,转载必须注明作者和本文链接
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!