0

How can I make Soap request using Curl php by using below soap format and url? I have tried avaliable solutions online and none of them worked out.

$soap_request = '<?xml version="1.0" encoding="UTF-8"?>
        <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:alisonwsdl" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
          <soap:Header>
            <credentials>
                <alisonOrgId>9ReXlYlpOThe24AWisE</alisonOrgId>
                <alisonOrgKey>C2owrOtRegikaroXaji</alisonOrgKey>
            </credentials>
          </soap:Header>
          <SOAP-ENV:Body>
            <q1:login xmlns:q1="urn:alisonwsdl">
                <email xsi:type="xsd:string">email</email>
                <firstname xsi:type="xsd:string">fname</firstname>
                <lastname xsi:type="xsd:string">lname</lastname>
                <city xsi:type="xsd:string">city</city>
                <country xsi:type="xsd:string">country</country>
                <external_id xsi:type="xsd:string"></external_id>
            </q1:login>
          </SOAP-ENV:Body>
        </SOAP-ENV:Envelope>';

 Url
https://alison.com/api/service.php?wsdl
curveball
  • 4,320
  • 15
  • 39
  • 49
Theodory Faustine
  • 432
  • 2
  • 10
  • 22
  • 1
    What exactly did you try & why didn't they work? Here's another answer for a similar scenario: https://stackoverflow.com/a/12222750/2173960. If you're still not getting desired output, then do post what you tried. Also, just in case you're only looking for a simple & free tool, try SoapUI. – Vineet Oct 02 '18 at 07:14
  • Note also that PHP has a built-in [SOAP Client](http://php.net/soapclient) which aims to help you connect to standard SOAP services. – IMSoP Oct 02 '18 at 08:39
  • 1
    Oh, and I really hope that isn't your real authentication key that you've just posted to a public website! – IMSoP Oct 02 '18 at 08:39
  • That's real authentication key @IMSoP – Theodory Faustine Oct 02 '18 at 08:46
  • For some reason I have tried to use SoapClient and clas not found error is returned though I have enabled soap extension – Theodory Faustine Oct 02 '18 at 08:47
  • @TheodoryFaustine Cool! Can I have your Stack Overflow password and your bank details as well please? ;) – IMSoP Oct 02 '18 at 08:53

2 Answers2

2

Assuming you already have php_soap extension installed, you can access the SOAP API like this:

<?php

$client = new SoapClient('https://alison.com/api/service.php?wsdl', array(
    'stream_context' => stream_context_create(array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false, 
            'allow_self_signed' => true
        )
    ))
));

You might want to define the header for authentication as well

$auth = array(
    'alisonOrgId' => '9ReXlYlpOThe24AWisE',
    'alisonOrgKey' => 'C2owrOtRegikaroXaji'
);

$header = new SoapHeader('https://alison.com/api/service.php?wsdl', 'credentials', $auth, false);
$client->__setSoapHeaders($header);

Then you can get the list of functions available

// Get function list
$functions = $client->__getFunctions ();
echo "<pre>";
var_dump ($functions);
echo "</pre>";
die;

Or call a function right away, like this:

// Run the function
$obj = $client->__soapCall("emailExists", array(
    "email" => "test@email.com"
));
echo "<pre>";
var_dump ($obj);
echo "</pre>";
die;
Adlan Arif Zakaria
  • 1,706
  • 1
  • 8
  • 13
  • Note that in most cases, you don't need to call `__soapCall`; in your example, you could write `$client->emailExists(array( "email" => "test@email.com"));` – IMSoP Oct 02 '18 at 09:31
  • I have credentials for that API on the header how do I attach them using SoapClient()? – Theodory Faustine Oct 03 '18 at 08:36
  • Thank alot Adlan but now am having this error "auth error", I don't know why this come from and I have configured everything correctly – Theodory Faustine Oct 03 '18 at 09:19
  • It is highlighting error is here when I try to use this metthod $client->getUserId('t****@gmail.com'); – Theodory Faustine Oct 03 '18 at 09:23
  • Change your parameter inside your header. The ```alisonOrgId``` and ```C2owrOtRegikaroXaji``` is just a sample – Adlan Arif Zakaria Oct 03 '18 at 10:03
  • I have already updated them to real one but nothin seems to work and when I remove header part some functions return data @Adlan Arif Zakaria – Theodory Faustine Oct 03 '18 at 11:06
  • @TheodoryFaustine Please do not expect users of an online forum to give you free private consultation. If you have further questions which would be a fit for this site as defined in the [help] then feel free to ask a new question. If you need more detailed help than that, I suggest you pay someone for their time. – IMSoP Oct 03 '18 at 11:48
  • @IMSoP I have realized that the problem comes from Soap header it is not authenticating correctly, How do I recognize namespace because I dont know how to set it – Theodory Faustine Oct 03 '18 at 13:00
  • @TheodoryFaustine This is not the right place to ask follow-up questions. Take the [tour] so you can understand how this site works and how to get the most out of it. – IMSoP Oct 03 '18 at 13:05
0

After struggling for a week I was able to find something in this tutorial here on youtube https://www.youtube.com/watch?v=6V_myufS89A and I was able to send requests to the API successifuly, first read my xml format above before continuing with my solution

     $options = array('trace'=> true, "exception" => 0);

    $client  = new \SoapClient('your url to wsdl',$options);

   //if you have Authorization parameters in your xml like mine above use SoapVar and SoapHeader as me below

    $params = new \stdClass();
    $params->alisonOrgId = 'value here';
    $params->alisonOrgKey = 'value here';

    $authentication_parameters = new \SoapVar($params,SOAP_ENC_OBJECT);

    $header = new \SoapHeader('your url to wsdl','credentials',$authentication_parameters);

    $client->__setSoapHeaders(array($header));


   print_r($client->__soapCall("Function here",'Function parameter here or left it as null if has no parameter'));

    //or call your function by

     print_r($client->yourFunction($function_parameters));

    }

Hope this will help someone out there struggling with soap requests that contains authentication informations

SagarPPanchal
  • 9,839
  • 6
  • 34
  • 62
Theodory Faustine
  • 432
  • 2
  • 10
  • 22