如何使用PHP构建正确的SOAP请求

如何使用PHP构建正确的SOAP请求

本文介绍了如何使用PHP构建正确的SOAP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要格式化/构建对此SOAP服务"的请求: http://api.notificationmessaging.com/NMSOAP/NotificationService?wsdl

I need to format/build a request for this SOAP "service":http://api.notificationmessaging.com/NMSOAP/NotificationService?wsdl

理想情况下,我想使用本机PHP SOAP类,但是我开始怀疑该类是否不是导致问题的原因.

Ideally I would like to use the native PHP SOAP class, but I'm beginning to wonder if this class is not the cause of my problems.

手册提供了以下示例:

    <soapenv:Body>
        <api:sendObject>
            <arg0>
                <content>
                    <entry>
                        <key>1</key>
                        <value>
                        <![CDATA[
                        <table width="600">
                        <tr>
                        <td>
                        <font size="2" face="Arial">Our powerful algorithms already
                        found a matching profile that matches your criteria:
                        <br>Celina72&nbsp;</font>
                        <img src="http://mypath/to/my/image.gif" width="50"
                        height="50" border="0" />
                        </td>]]></value>
                    </entry>
                </content>
                <dyn>
                    <entry>
                        <key>FIRSTNAME</key>
                        <value>john</value>
                    </entry>
                </dyn>
                <email>[email protected]</email>
                <encrypt>BdX7CqkmjSivyBgIcZoN4sPVLkx7FaXGiwsO</encrypt>
                <notificationId>6464</notificationId>
                <random>985A8B992601985A</random>
                <senddate>2008-12-12T00:00:00</senddate>
                <synchrotype>NOTHING</synchrotype>
                <uidkey>EMAIL</uidkey>
            </arg0>
        </api:sendObject>
    </soapenv:Body>
</soapenv:Envelope>

这是我的PHP请求产生的垃圾(来自__getLastRequest())

Here is the garbage that my PHP request produces (from __getLastRequest())

<?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://api.service.nsapi.emailvision.com/" xmlns:ns2="http://xml.apache.org/xml-soap">
        <SOAP-ENV:Body>
            <ns1:sendObject/>
            <param1>AAAAAAAAAAAAAAAAAAAAAAAAAAA</param1>
            <param2>123456789</param2>
            <param3>BBBBBBBBBBBB</param3>
            <param4>2013-09-09T00:00:00</param4>
            <param5>NOTHING</param5>
            <param6>EMAIL</param6>
            <param7>
                <ns2:Map>
                    <item>
                        <key>2</key>
                        <value>TEST</value>
                    </item>
                </ns2:Map>
            </param7>
            <param8>
                <ns2:Map>
                <item>
                    <key>FIRSTNAME</key>
                    <value>John</value>
                </item>
            </ns2:Map>
            <ns2:Map>
                <item>
                    <key>LASTNAME</key>
                    <value>Smith</value>
                </item>
            </ns2:Map>
        </param8>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我的电话:

$client = new SoapClient('http://api.notificationmessaging.com/NMSOAP/NotificationService?wsdl', array( 'trace' => 1, 'exceptions' => 0 ) );

参数看起来像这样(用伪数据修改):

The params look like this (modified with dummy data):

$email = '[email protected]';
$encrypt = 'AAAAAAAAAAAAAAAAAAAAAAAAAAA';
$notification_id = 123456789;
$random = 'BBBBBBBBBBBB';
$senddate = '2013-09-09T00:00:00';
$synchrotype = 'NOTHING';
$uidkey = 'EMAIL';

$content = array();
$content[] = array(
    2 => 'TEST'
);

$dyn = array();
$dyn[] = array(
    'FIRSTNAME' => 'John'
);
$dyn[] = array(
    'LASTNAME' => 'Smith'
);

$params = array(
    'email' => $email,
    'encrypt' => $encrypt,
    'notificationId' => $notification_id,
    'random' => $random,
    'senddate' => $senddate,
    'synchrotype' => $synchrotype,
    'uidkey' => $uidkey,
    'content' => $content,
    'dyn' => $dyn
);

然后我像这样执行请求:

I then execute the request like this :

$res = $client->__soapCall( 'sendObject', array( $email, $encrypt, $notification_id, $random, $senddate, $synchrotype, $uidkey, $content, $dyn ) );

为什么PHP无法正确格式化我的请求?有一种直接的方法,我可以手工编写" XML,然后使用cURL将其发布?

Why is PHP unable to format my request correctly? Is there a mor direct approach where I could write the XML "by hand" and then post it using cURL?

推荐答案

是否有更直接的方法可以编写XML?"

"Is there a more direct approach where I could write the XML?"

通过使用 SoapVar 并设置编码参数为 XSD_ANYXML 即可编写原始XML.

By using a SoapVar and setting the encode parameter of the constructor to XSD_ANYXML you can write the raw XML.

WSDL应该有一种方法可以帮助构建XML.

There should be a way where the WSDL helps build the XML though.

您可以尝试这样的事情:

You could try something like this:

$wsdl   = "http://api.notificationmessaging.com/NMSOAP/NotificationService?wsdl";
$client = new SoapClient($wsdl, array(  'soap_version' => SOAP_1_1,
                                        'trace' => true,
                                      ));
try {

    $xml = '<arg0>
            <content>
                <entry>
                    <key>1</key>
                    <value>
                    <![CDATA[
                    <table width="600">
                    <tr>
                    <td>
                    <font size="2" face="Arial">Our powerful algorithms already
                    found a matching profile that matches your criteria:
                    <br>Celina72&nbsp;</font>
                    <img src="http://mypath/to/my/image.gif" width="50"
                    height="50" border="0" />
                    </td>]]></value>
                </entry>
            </content>
            <dyn>
                <entry>
                    <key>FIRSTNAME</key>
                    <value>john</value>
                </entry>
            </dyn>
            <email>[email protected]</email>
            <encrypt>BdX7CqkmjSivyBgIcZoN4sPVLkx7FaXGiwsO</encrypt>
            <notificationId>6464</notificationId>
            <random>985A8B992601985A</random>
            <senddate>2008-12-12T00:00:00</senddate>
            <synchrotype>NOTHING</synchrotype>
            <uidkey>EMAIL</uidkey>
        </arg0>';

    $args = array(new SoapVar($xml, XSD_ANYXML));
    $res  = $client->__soapCall('sendObject', $args);
    return $res;
} catch (SoapFault $e) {
    echo "Error: {$e}";
}

echo "<hr>Last Request";
echo "<pre>", htmlspecialchars($client->__getLastRequest()), "</pre>";

这篇关于如何使用PHP构建正确的SOAP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 05:53