問(wèn)題描述
盡管有一段時(shí)間是 PHP 開(kāi)發(fā)人員,但我現(xiàn)在才第一次體驗(yàn) Web 服務(wù).我希望能得到一點(diǎn)幫助,因?yàn)槲艺谑褂玫倪@本書(shū)幫助不大.與我們有業(yè)務(wù)往來(lái)的其中一家公司給了我一個(gè) XML 文檔,其格式為 in需要(我將發(fā)布其中的一部分).由于我在這個(gè)特定主題上缺乏經(jīng)驗(yàn),我不確定該怎么做.我需要知道如何將此消息發(fā)送到他們的實(shí)時(shí) POST 頁(yè)面,如何接收響應(yīng),我是否需要?jiǎng)?chuàng)建任何類(lèi)型的 WSDL 頁(yè)面?任何幫助或指導(dǎo)將不勝感激,請(qǐng)不要只發(fā)送指向 php 手冊(cè)的鏈接.我顯然去過(guò)那里,因?yàn)樗ǔJ菍で髱椭氖走x場(chǎng)所.
Despite being a PHP developer for a while, I'm just now getting my first taste of web services. I was hoping to get a little help, as the book I am using is not much help. One of the companies we are doing business with gave me an XML document in the format in needs to be in (I'll post a chunk of it). Due to my inexperience in this particular subject, I'm not really sure what to do. I need to know how to send this message to their live POST page, how to receive the response, and do I need to create any sort of WSDL page? Any help or direction would be so greatly appreciated, and please, don't just send a link to the php manual. I've obviously been there, as it is typically the go-to place for help.
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 文檔(我刪除了大部分正文).請(qǐng)讓我知道是否需要更多細(xì)節(jié),因?yàn)槲蚁氡M可能具體,以便我能夠弄清楚如何發(fā)送此
Above is the AddOrder XML document I was given (I removed most of the body). Please let me know if anymore detail is needed, as I want to be specific as possible so I'm able to figure out how send this
推薦答案
您有幾個(gè)選擇!您可以使用soap 對(duì)象來(lái)創(chuàng)建基于WSDL 的請(qǐng)求,該請(qǐng)求將知道與遠(yuǎn)程服務(wù)器通信的正確方式.您可以在 PHP 手冊(cè)中了解如何執(zhí)行此操作.
You have a couple of options! You could use soap objects to create the request which, based upon a WSDL will know the correct way to talk to the remote server. You can see how to do this at the PHP manual.
或者,您可以使用 CURL 來(lái)完成這項(xiàng)工作.您需要知道將數(shù)據(jù)發(fā)布到哪里(它看起來(lái)像上面的示例),然后您可以執(zhí)行以下操作:
Alternatively, you can use CURL to do the work. You'll need to know where to post the data to (which it looks like is in the example above), then you can just do something like this:
$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);
然后您應(yīng)該在 $result 變量中得到結(jié)果.然后您可以嘗試將其轉(zhuǎn)換為 XML 文檔,盡管有時(shí)我發(fā)現(xiàn)由于編碼而無(wú)法正常工作:
You then should have the result in the $result var. You can then try to convert it to an XML doc, although sometimes I've found due to encoding this doesn't work:
$xml = new SimpleXMLElement($result);
print_r($xml);
這篇關(guān)于PHP SOAP HTTP 請(qǐng)求的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!