[英]How to easily consume a web service from PHP
在PHP 5中,您可以在WSDL上使用SoapClient來調用Web服務功能。 例如 :
$client = new SoapClient("some.wsdl");
和$ client現在是一個對象,它具有some.wsdl中定義的類方法。 因此,如果在WSDL中有一個名為getTime的方法,那么您只需調用:
$result = $client->getTime();
並且結果將(顯然)在$ result變量中。 您可以使用__getFunctions方法返回所有可用方法的列表。
我用wsdl2php取得了很大的成功。 它將自動為Web服務中使用的所有對象和方法創建包裝類。
我過去使用過NuSOAP 。 我喜歡它,因為它只是一組可以包含的PHP文件。 Web服務器上無需安裝任何內容,也無需更改配置選項。 它也有WSDL支持,這是一個獎勵。
本文介紹如何使用PHP SoapClient調用api Web服務。
我從這個網站得到了這個: http : //forums.asp.net/t/887892.aspx?Consume + an + ASP + Internet + Web + Service+with+PHP
Web服務有方法Add
,它有兩個參數:
<?php
$client = new SoapClient("http://localhost/csharp/web_service.asmx?wsdl");
print_r( $client->Add(array("a" => "5", "b" =>"2")));
?>
假設您提供以下內容:
<x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" xmlns:int="http://thesite.com/">
<x:Header/>
<x:Body>
<int:authenticateLogin>
<int:LoginId>12345</int:LoginId>
</int:authenticateLogin>
</x:Body>
</x:Envelope>
和
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<authenticateLoginResponse xmlns="http://thesite.com/">
<authenticateLoginResult>
<RequestStatus>true</RequestStatus>
<UserName>003p0000006XKX3AAO</UserName>
<BearerToken>Abcdef1234567890</BearerToken>
</authenticateLoginResult>
</authenticateLoginResponse>
</s:Body>
</s:Envelope>
讓我們說訪問http://thesite.com/說WSDL地址是: http : //thesite.com/PortalIntegratorService.svc?wsdl
$client = new SoapClient('http://thesite.com/PortalIntegratorService.svc?wsdl');
$result = $client->authenticateLogin(array('LoginId' => 12345));
if (!empty($result->authenticateLoginResult->RequestStatus)
&& !empty($result->authenticateLoginResult->UserName)) {
echo 'The username is: '.$result->authenticateLoginResult->UserName;
}
如您所見,盡管可以更改LoginId值,但在PHP代碼中使用XML中指定的項。
嗯,這些功能特定於您用於這些語言開發的工具。
如果(例如)您使用記事本編寫代碼,則不會使用這些工具。 所以,也許你應該問你正在使用的工具的問題。
對於PHP: http : //webservices.xml.com/pub/a/ws/2004/03/24/phpws.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.