PHP SOAP HTTP请求 [英] PHP SOAP HTTP Request

查看:119
本文介绍了PHP SOAP HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管作为PHP开发人员已有一段时间,但我现在才刚开始体验Web服务.我希望得到一点帮助,因为我正在使用的书没有太大帮助.我们与之开展业务的公司之一给我提供了一个XML文档,其格式为需要放入的格式(我将在其中贴出一部分).由于我在这个特定主题上的经验不足,因此我不确定该怎么做.我需要知道如何将此消息发送到他们的实时POST页面,如何接收响应以及是否需要创建任何类型的WSDL页面?任何帮助或指导将不胜感激,请不要仅仅发送链接到php手册.我显然去过那里,因为它通常是寻求帮助的地方.

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文档(我删除了大部分内容).请让我知道是否需要更多详细信息,因为我想尽可能具体,所以我可以弄清楚如何发送此信息

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

推荐答案

您有两种选择!您可以使用soap对象创建请求,该请求基于WSDL将知道与远程服务器进行通信的正确方法.您可以在 PHP手册中查看如何执行此操作.

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来完成工作.您需要知道将数据发布到何处(如上例所示),然后您可以执行以下操作:

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);

然后您应该将结果保存到$ result变量中.然后,您可以尝试将其转换为XML文档,尽管有时由于编码我发现它不起作用:

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);

这篇关于PHP SOAP HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆