0

我正在尝试使用 SOAP 的东西,但我根本不懂 SOAP。我使用原生 PHP 类,但总是收到 sae 错误:“处理时发生故障。” 用这个描述:“java.lang.NullPointerException”。

我打的电话是这样开始的:

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

然后参数看起来像这样(用虚拟数据修改)

$email = 'john.smith@example.com';
$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
);

然后我执行这样的请求:

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

我不知道从哪里开始。我尝试了几种方法来执行请求,但它从来没有奏效。

我已经在 PHP 中成功使用了几个 REST 接口,但 SOAP 对我来说毫无意义。我已经阅读了基本教程,但我仍然不知道,特别是因为它们显然不适用于我的情况。

环境有效

你可能会问,所以:是的,SOAP 正在服务器上工作,因为这个请求返回一个有效的响应:

$requestParams = array(
    'CityName' => 'Berlin',
    'CountryName' => 'Germany'
);

$client = new SoapClient( 'http://www.webservicex.net/globalweather.asmx?WSDL' );
$response = $client->GetWeather( $requestParams );

print_r( $response );

这给了我类似的东西:

<CurrentWeather>
  <Location>Berlin-Tegel, Germany (EDDT) 52-34N 013-19E 37M</Location>
  <Time>Sep 10, 2013 - 09:20 AM EDT / 2013.09.10 1320 UTC</Time>
  <Wind> from the SW (230 degrees) at 9 MPH (8 KT) (direction variable):0</Wind>
  <Visibility> 4 mile(s):0</Visibility>
  <SkyConditions> mostly cloudy</SkyConditions>
  <Temperature> 59 F (15 C)</Temperature>
  <DewPoint> 55 F (13 C)</DewPoint>
  <RelativeHumidity> 87%</RelativeHumidity>
  <Pressure> 29.85 in. Hg (1011 hPa)</Pressure>
  <Status>Success</Status>
</CurrentWeather>

很高兴知道,但我仍然无法满足自己的要求……请不要让它变成风滚草,我已经拿到了徽章!谢谢。

编辑

这是手册中提供的示例:

    <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>johnblum@flowerpower.com</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>

这是我的请求产生的垃圾(来自 __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>

电子邮件去哪儿了?

4

1 回答 1

3

您的错误消息来自服务器端。你的参数有问题。

<xs:element name="content">
    <xs:complexType>
        <xs:sequence>
            <xs:element maxOccurs="unbounded" minOccurs="0" name="entry">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="key" type="xs:int"/>
                        <xs:element minOccurs="0" name="value" type="xs:string"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

所以基本上API正在等待这样的事情:

...
<content>
    <entry>
        <key>xs:int</key>
        <value>xs:string</key>
    </entry>
</content>
...

尝试这样的事情:

$content[] = array(
    'entry' => array(
        array(
            'key'   => 2,
            'value' => 'TEST',
        ),
    ),
);

另外 $dyn 构建不正确。

--- EDIT after this ---

好的,您的要求解释了很多。直接使用 __soapCall() 而不告诉键会导致您的请求像这样构建。这可以帮助你:

$request = array(
    'arg0' => array(
        'content' => array(
            'entry' => array(
                'key'   => 1,
                'value' => 'TESTING',
            ),
        ),
        'dyn' => array(
            'entry' => array(
                'key'   => 'FIRSTNAME',
                'value' => 'john',
            ),
        ),
        'email'             => 'johnblum@flowerpower.com',
        'encrypt'           => 'BdX7CqkmjSivyBgIcZoN4sPVLkx7FaXGiwsO',
        'notificationId'    => 6464,
        'random'            => '985A8B992601985A',
        'senddate'          => '2008-12-12T00:00:00',
        'synchrotype'       => 'NOTHING',
        'uidkey'            => 'EMAIL',
    ),
);

$result = $client->__soapCall( 'sendObject', array($request) );

还注意到有一个“神奇的” arg0 包装了请求。希望这可以帮助你!我还建议探索名为 SoapUI 的软件。它允许您从 WSDL 生成模拟服务,并使您在调试时生活得更轻松。

于 2013-09-10T14:29:33.873 回答