How to build a correct SOAP request with PHP

21,455

Solution 1

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

By using a SoapVar and setting the encode parameter of the constructor to XSD_ANYXML you can write the raw 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>";

Solution 2

i know that the topic is about 1 year old, but i find some good informations in it, that give me a very good help , and i finaly succed to make it work with the php method sendObject() so i hope my contribution will help others people too...

at first the call of sendObject() this way : $client->__soapCall seems not working at all you have to call it directly : $client->sendObject

in this topic, i think that it is a using of the API of emailvision (smartfocus, now...) this method sendObject does not need a generated token by openApiConnection()

oki, now , it is the code to make it work

<?php


    $email = 'johann.******@gmail.com';
    $encrypt = '******************************';
    $notification_id = '**************';
    $random = '********************';
    $senddate = '2013-09-09T00:00:00';
    $synchrotype = 'NOTHING';
    $uidkey = 'EMAIL';


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


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

    $res = $client->sendObject( $params );

echo "<br /><br /><br />";
echo "REQUEST 1 :" . htmlspecialchars($client->__getLastRequest()) . "<br />";
echo "RESPONSE 1 :" . htmlspecialchars($client->__getLastResponse()) . "<br /><br /><br />";

?>

you have to know that $encrypt ,$notification_id , $random are generated by creating a transactionnal message, you can get this informations in the interface of campagn commander

take care of the schema of input xml, there is a node arg0, then you have to make a level arg0 in your array parameters

to make it work directly with xml :

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

    $xml = '

            <ns1: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>johann*******@gmail.com</email>
                    <encrypt>*********************</encrypt>
                    <notificationId>**************</notificationId>
                    <random>**********************</random>
                    <senddate>2008-12-12T00:00:00</senddate>
                    <synchrotype>NOTHING</synchrotype>
                    <uidkey>EMAIL</uidkey>

                </arg0>
            </ns1:sendObject>



        ';

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

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

?>

it s important to write the first node like this : <ns1:sendObject>

<api:sendObject> does not work

Share:
21,455
Sébastien
Author by

Sébastien

Full stack JavaScript Web &amp; mobile developer: Ionic Angular NodeJS MongoDB I used to do PHP/MySQL as well ;)

Updated on July 18, 2022

Comments

  • Sébastien
    Sébastien almost 2 years

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

    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.

    The manual provides this example:

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

    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>
    

    My call:

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

    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?

  • Sébastien
    Sébastien over 10 years
    Thanks @quaspas, I like this approach much better. I tried this and I finaly got a response from the service. However it still does not work: I get the message "Message part arg0 was not recognized. (Does it exist in service WSDL?)". I Tried removing part but then the message is "Message part content was not recognized. (Does it exist in service WSDL?)". I may not be far from the solution, thanks to you :)
  • Sébastien
    Sébastien over 10 years
    Got it! All that was missing was an enclosing <ns1:sendObject>. Now the other problem was that the documentation uses the namespace "api" for the API, but PHP returns it as "ns1". Why? "Forget it, Jake. It's SOAP." Thanks again @quaspas for your great help!
  • IMSoP
    IMSoP almost 10 years
    In case anyone's curious about the unanswered question in the preceding comment, the prefixes ns1 and api don't by themselves mean anything; the important part is the URI it's bound to, using the xmlns:ns1="http://api.service.nsapi.emailvision.com/" attribute at the top of the document. You can give it whatever prefix you want by changing that attribute, or adding your own further at any point in the document.
  • Sparatan117
    Sparatan117 over 9 years
    Holy bloody hell. This is exactly what I needed. Thanks so much
  • Mosin
    Mosin almost 6 years
    thank you so much this code worked for me i was searching for this since from 30+ days
  • Adarsh M Pallickal
    Adarsh M Pallickal over 3 years
    I am getting the following error. Error: SoapFault exception: [Client] Function ("sendObject") is not a valid method for this service in. Could you please help me?