使用PHP的cURL和SoapClient实现WebService同步调用
一、WebService基础概念
WebService是一种跨平台、跨语言的远程调用技术。为了实现不同系统间的数据交换,WebService应运而生。SOAP、WSDL和UDDI构成了WebService的核心技术。
- SOAP(简单对象访问协议):基于XML的数据传输协议,用于定义数据传输格式。
- WSDL(WebService描述语言):描述服务接口、数据类型、服务地址等信息,可通过工具解析。
- UDDI(统一描述、发现和集成):用于存储和查找WebService描述文件,充当服务目录。
这些技术虽新,但其实基于已有技术实现,理解其原理后即可轻松上手。
二、PHP实现同步调用
由于同步调用需求,我们可以使用cURL来实现。以下是示例代码:
private function httpRequestXml($url, $data = null, $headers = null) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
if (!empty($headers)) {
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
$xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:app="http://siebel.com/Voucher/Apply">
<soapenv:Header/>
<soapenv:Body>
<app:VoucherApply_Input>
<app:count>1</app:count>
<app:memberNumber>'.$this->memberNumber.'</app:memberNumber>
</app:VoucherApply_Input>
</soapenv:Body>
</soapenv:Envelope>';
$headers[] = 'Content-Type: text/xml';
$headers[] = 'SOAPAction: "document/http://siebel.com/Voucher/Apply:VoucherApply"';
$result = $this->httpRequestXml($url, $xml, $headers);
if (strpos($result, "<ns:flag>S</ns:flag>")) {
// 处理成功响应
}
PHP5实现方法
-
启用Soap扩展: 在
php.ini中找到extension=php_soap.dll,去掉注释符号,重启PHP服务。 -
获取服务接口信息:
try {
$client = new SoapClient("http://example.com/services?wsdl");
print_r($client->__getFunctions()); // 获取可用方法
print_r($client->__getTypes()); // 获取数据类型
} catch (SoapFault $e) {
echo $e->getMessage();
}
- 调用服务方法:
try {
$client = new SoapClient('http://example.com/services?wsdl');
$xml = "<xml>...</xml>";
$result = $client->serviceMethod($xml);
print_r($result);
} catch (SoapFault $e) {
print_r('错误:' . $e->getMessage());
}
三、WSDL文件详解
WSDL文件定义了WebService的接口和数据格式。以下是WSDL文件示例:
<?xml version="1.0" encoding="utf-8"?>
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/services">
<types>
<xsd:schema targetNamespace="http://example.com/services">
<xsd:element name="ServiceRequest" type="xsd:string"/>
<xsd:element name="ServiceResponse" type="xsd:string"/>
</xsd:schema>
</types>
<message name="RequestMessage">
<part name="ServiceRequest" element="tns:ServiceRequest"/>
</message>
<message name="ResponseMessage">
<part name="ServiceResponse" element="tns:ServiceResponse"/>
</message>
<portType name="ServicePortType">
<operation name="ServiceOperation">
<input message="tns:RequestMessage"/>
<output message="tns:ResponseMessage"/>
</operation>
</portType>
<binding name="ServiceBinding" type="tns:ServicePortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="ServiceOperation">
<soap:operation soapAction="http://example.com/services/ServiceOperation"/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="ExampleService">
<port binding="tns:ServiceBinding" name="ServicePort">
<soap:address location="http://example.com/services"/>
</port>
</service>
</definitions>
四、使用cURL调用WebService获取天气信息
以下是一个使用cURL调用天气WebService的示例:
<?php
$data = 'theCityCode=864&theUserID=';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.webxml.com.cn/WebServices/WeatherWS.asmx/getWeather');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$headers = array(
'Content-Type: application/x-www-form-urlencoded;charset=utf-8',
'Content-Length: ' . strlen($data),
'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36'
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
if (!curl_errno($curl)) {
echo $response;
} else {
echo 'curl 错误:' . curl_errno($curl);
}
curl_close($curl);
?>
以上代码展示了如何使用cURL调用WebService接口,获取天气信息。