一个简单的PHP SOAP请求因java.lang.NullPointerException而失败

时间:2013-09-10 14:11:33

标签: php web-services soap wsdl

我正在尝试使用SOAP,但我根本不理解SOAP。我使用本机PHP类,但我总是得到sae错误:“处理时出错”。有了这个描述:“java.lang.NullPointerException”。

我打电话的开头是:

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

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

$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>

电子邮件去了哪里?

1 个答案:

答案 0 :(得分: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生成模拟服务,并使您在调试时更容易生活。