PHP SOAP HTTP 请求

发布于 2024-12-26 02:57:41 字数 1809 浏览 2 评论 0原文

尽管我已经成为 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 文档(我删除了大部分正文)。如果需要更多详细信息,请告诉我,因为我想尽可能具体,以便我能够弄清楚如何发送此信息

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>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撩心不撩汉 2025-01-02 02:57:41

您有几个选择!您可以使用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 中得到结果变种然后您可以尝试将其转换为 XML 文档,尽管有时我发现由于编码而不起作用:

$xml = new SimpleXMLElement($result);
print_r($xml);

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.

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

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);
悲喜皆因你 2025-01-02 02:57:41

作为服务的提供商,另一家公司应向您提供 WSDL 文档,以计算机可读的术语描述服务。通常,它们是通过 http://their.service.url/wsdl 或类似的 url 提供的。

一旦完成,您应该能够创建一个 SoapClient 实例来与服务交互。

As the provider of the service, the other company should supply you with a WSDL document which describes the service in computer readable terms. Typically they are provided via an url like http://their.service.url/wsdl or similar.

Once you have that you should be able to create a SoapClient instance to interact with the service.

没有伤那来痛 2025-01-02 02:57:41

嗯,这绝对是一个 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();

}

Well this is definitely a SOAP request, so you'll need to use SOAP to work correctly with this or you are in for a major headache.

I've had several encounter with SOAP and PHP and everytime i had to rely on an external library. The most recent one i had to use was Zend_Soap_Client.

But then again, do you have the WSDL available? You need a WSDL to be able to use a SOAP webservice using a client library.

http://framework.zend.com/manual/en/zend.soap.html

And here is a sample of my code i used, i hope it'll get you started

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文