尽管做了一段时间的PHP开发人员,但我现在才第一次体验到web服务。我希望能得到一点帮助,因为我正在使用的这本书帮助不大。与我们有业务往来的一家公司给了我一个XML文档,该文档的格式为所需格式(我将发布一大块)。由于我在这方面缺乏经验,我真的不知道该怎么办。我需要知道如何将此消息发送到他们的实时POST页面,如何接收响应,以及我需要创建任何类型的WSDL页面吗?任何帮助或指导都将不胜感激,请不要只是发送php手册的链接。我显然去过那里,因为那里通常是寻求帮助的地方。
POST /sample/order.asmx HTTP/1.1
Host: orders.sample.com
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?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:Header>
<AuthenticationHeader xmlns="http://sample/">
<Username>string</Username>
<Password>string</Password>
</AuthenticationHeader>
<DebugHeader xmlns="http://sample/">
<Debug>boolean</Debug>
<Request>string</Request>
</DebugHeader>
</soap12:Header>
<soap12:Body>
<AddOrder xmlns="http://sample/">
<order>
<Header>
<ID>string</ID>
<EntryDate>dateTime</EntryDate>
<OrderEntryView>
<SeqID>int</SeqID>
<Description>string</Description>
</OrderEntryView>
<ReferenceNumber>string</ReferenceNumber>
<PONumber>string</PONumber>
<Comments>string</Comments>
<IpAddress>string</IpAddress>
</Header>
</order>
</AddOrder>
</soap12:Body>
</soap12:Envelope>
上面是我得到的AddOrder XML文档(我删除了大部分正文)。请让我知道是否需要更多的细节,因为我想尽可能具体,这样我就可以弄清楚如何发送这个
您有几个选项!您可以使用soap对象来创建请求,该请求将基于WSDL知道与远程服务器进行通信的正确方式。您可以在PHP手册中看到如何做到这一点。
或者,您可以使用CURL来完成工作。你需要知道将数据发布到哪里(在上面的例子中看起来是这样的),然后你可以做这样的事情:
$curlData = "<?xml version="1.0" encoding="utf-8"?>... etc";
$url='http://wherever.com/service/';
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl,CURLOPT_TIMEOUT,120);
curl_setopt($curl,CURLOPT_ENCODING,'gzip');
curl_setopt($curl,CURLOPT_HTTPHEADER,array (
'SOAPAction:""',
'Content-Type: text/xml; charset=utf-8',
));
curl_setopt ($curl, CURLOPT_POST, 1);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $curlData);
$result = curl_exec($curl);
curl_close ($curl);
然后应该在$result var中有结果。然后你可以尝试将其转换为XML文档,尽管有时我发现由于编码原因,这不起作用:
$xml = new SimpleXMLElement($result);
print_r($xml);
作为服务的提供者,其他公司应该向您提供一个WSDL文档,该文档以计算机可读的术语描述服务。通常,它们是通过类似的url提供的http://their.service.url/wsdl或类似的。
一旦您拥有了SoapClient
实例,就应该能够创建一个实例来与服务交互。
这肯定是一个SOAP请求,所以您需要使用SOAP来正确处理它,否则您将非常头疼。
我遇到过几次SOAP和PHP,每次都不得不依赖外部库。我最近不得不使用的是Zend_Soap_Client。
但话说回来,您有可用的WSDL吗?您需要一个WSDL,以便能够使用客户端库使用SOAP Web服务。
http://framework.zend.com/manual/en/zend.soap.html
这是我使用的代码示例,我希望它能让你开始
ini_set('soap.wsdl_cache_enabled', 0);
set_include_path(dirname(__FILE__).'/../../libraries/:'.get_include_path());
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
//Include the classes for the webservice
include('CatalogOrder.php');
include('CatalogOrderItem.php');
include('CatalogOrderWebservice.php');
//Check the mode
if(isset($_GET['wsdl'])) {
$autodiscover = new Zend_Soap_AutoDiscover(array(
'classmap' => array(
'CatalogOrder' => "CatalogOrder",
'CatalogOrderItem' => "CatalogOrderItem"
)
));
$autodiscover->setComplexTypeStrategy(new Zend_Soap_Wsdl_Strategy_ArrayOfTypeComplex());
$autodiscover->setClass('CatalogOrderWebService');
$autodiscover->handle();
//Return the consume form and process the actions of the consumer
} elseif(isset($_GET['consume'])) {
// pointing to the current file here
$soap = new Zend_Soap_Client("http://".$_SERVER['HTTP_HOST']."/admin/export/WebService.php?wsdl", array(
'classmap' => array(
'CatalogOrder' => "CatalogOrder",
'CatalogOrderItem' => "CatalogOrderItem"
),
'encoding' => 'iso-8859-1'
));
include('CatalogOrderWebserviceConsumer.php');
//Process SOAP requests
} else {
// pointing to the current file here
$soap = new Zend_Soap_Server("http://".$_SERVER['HTTP_HOST']."/admin/export/WebService.php?wsdl", array(
'classmap' => array(
'CatalogOrder' => "CatalogOrder",
'CatalogOrderItem' => "CatalogOrderItem"
),
'encoding' => 'iso-8859-1'
));
$soap->setClass('CatalogOrderWebService');
$soap->handle();
}