Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SOAP with PHP, simple issue

Tags:

php

soap

I am trying to integrate an API into my site (the API is not important, the issue is with SOAP). I do not normally code in PHP, but focus on javascript, so SOAP and related things are pretty foreign to me. I have been searching and trying different things for about 2 and a half hours, and managed to narrow down my problem.

I called $client->__getFunctions(), which gives me a list of strings defining all of the functions I am able to use. Here is the string of the function I would like to use:

"GetActivationCodeResponse GetActivationCode(GetActivationCode $parameters)"

I have written and am writing in convention with the API creators, as this is my first time using SOAP, and first time using php in a while.

So I created a function in my class named GetActivationCode that looks like this:

public function GetActivationCode($params) {
    $this->client->GetActivationCode($params);
    var_dump($this->client);
}

This will always output the SOAP error:

Server was unable to process request. --->
System.NullReferenceException:  
Object reference not set to an instance of an object

So I am guessing that it expects the passed parameter to be passed to be an instance of a class called GetActivationCode? I do not know how to do this, and would not like to create a whole new class to serve one function (but I will if that is the solution).

Class I Have Written

<?php

require_once("../includes/mbApi.php");
error_reporting(E_ALL);
ini_set('display_errors', '1');

class MBActivateService extends MBAPIService 
{
    function __construct($debug = false)
    {
        $serviceUrl = "http://" . GetApiHostname() . "/0_5/SiteService.asmx?wsdl";

        $this->debug = $debug;
        $option = array();
        if ($debug)
        {
            $option = array('trace'=>1);
        }
        $this->client = new soapclient($serviceUrl, $option);
    }

    public function GetActivationCode($s, $k, $ids) {
        var_dump($this->client->__getFunctions());
        $arr = array();
        $arr["SourceName"] = $s;
        $arr["Password"] = $k;
        $arr["SiteIDs"] = $ids;
        var_dump($arr);
        $this->client->GetActivationCode($arr);
    }

}

$activate = new MBActivateService();

$result = $activate->GetActivationCode("She3", "private api key", array("28856"));

var_dump($result);

?>

Overall Goal

This is the overall goal, in case someone can offer a better solution. I need to send the following SOAP request:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://clients.mindbodyonline.com/api/0_5">
   <soapenv:Header/>
   <soapenv:Body>
      <GetActivationCode>
         <Request>
            <SourceCredentials>
               <SourceName>XXXX</SourceName>
               <Password>XXXX</Password>
               <SiteIDs>
                  <int>XXXX</int>
               </SiteIDs>
            </SourceCredentials>
         </Request>
      </GetActivationCode>
   </soapenv:Body>
</soapenv:Envelope>

I need to send the options for SourceName, Password, and SiteIDs (array).

Thanks in advance for any input!

like image 677
Austin Avatar asked Sep 12 '26 07:09

Austin


1 Answers

This sounds familiar. The first thing I would try is to rewrite your function to this:

public function GetActivationCode($params) {
    var_dump($this->client->GetActivationCode(array(
        'Request' => array(
            'SourceCredentials' => $params
        ),
    )));
}

Also, to debug you can add this to the options of the SoapClient constructor:

array(
    'trace' => true,
)

This way you can do $this->client->__getLastRequest() to debug your code.

like image 149
Ja͢ck Avatar answered Sep 13 '26 23:09

Ja͢ck