跨系統的資料交換與溝通, MIS常會提供.NET Webservice :

Image 20

 

在php中如何發送soap 呢? 如何發送請求呢? 最簡單的方式如下, 紅色文字自行變更, 應輸出XML格式:

header('Content-Type: text/xml; charset=UTF-8'); //避免中文亂碼且輸出為 xml
$client = new SoapClient("http://www.XXX.com/GetService.asmx?WSDL", array('trace' => 1)); //網址請自行變更
$result = $client->GetCompanyCode(); //要呼叫的方法,請自行變更
echo $client->__getLastResponse();

如果順利印出xml, 可到此驗證xml: http://www.w3schools.com/xml/xml_validator.asp

 

另一個方式, 取得格式不同:


header('Content-Type: text/html; charset=UTF-8'); //避免中文亂碼
$client = new SoapClient("http://xxx.xxx.com/GetService.asmx?WSDL");  //網址請自行變更

//要傳入的參數,請自行變更,如不需傳入參數,則下方紅色字全部移除
$params = array(
  "Contact" => $contact,
  "description" => "Barrel of Oil",
  "amount" => 500,
);

$response = $client->__soapCall("Function1", array($params)); //要呼叫的方法,請自行變更

var_dump($response);
 

 

如果提供service的MIS 需要你傳入一段XML當作參數, 而非傳入一個ARRAY呢?


header('Content-Type: text/xml; charset=UTF-8'); //避免中文亂碼且輸出為 xml

$url = 'http://www.xxx.com/GetService.asmx?WSDL';
$client = new SoapClient($url);

$xmlr = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><XXXX><Command><ID>30</ID></Command></XXXX>');  //自行更改要傳入的XML


$params = new stdClass();
$params->xml = $xmlr->asXML();

$result = $client->方法名稱($params); //自行更改方法名稱
echo $result->方法名稱; //自行更改方法名稱
 

分享這篇文章的網址這篇文章的網址(可分享到FB、LINE):

用LINE分享給朋友: