1

I'm trying to create a soap request in PHP and am new to both. I have:

<?php

define("PRODUCTION_KEY", "YOUR_PRODUCTION_KEY_HERE");
define("PRODUCTION_PASS", "YOUR_PRODUCTION_PASS_HERE");
define("BILLING_ACCOUNT", "YOUR_ACCOUNT_HERE");
define("REGISTERED_ACCOUNT", "YOUR_ACCOUNT_HERE");
define("USER_TOKEN", "YOUR_USER_TOKEN_HERE");

function createPWSSOAPClient(){
  $client = new SoapClient( "./wsdl/trackingservice.wsdl", 
                            array   (
                                    'trace'         =>  true,
                                    'location'  =>  "https://example.com",
                                    'uri'               =>  "http://example.com/datatypes/v1",
                                    'login'         =>  PRODUCTION_KEY,
                                    'password'  =>  PRODUCTION_PASS
                                  )
                          );
  //Define the SOAP Envelope Headers
  $headers[] = new SoapHeader ( 'http://example.com/datatypes/v1', 
                                'RequestContext', 
                                array (
                                        'Version'           =>  '1.2',
                                        'Language'          =>  'en',
                                        'GroupID'           =>  'xxx',
                                        'RequestReference'  =>  'Rating Example',
                                        'UserToken'         =>  USER_TOKEN
                                      )
                              ); 
  $client->__setSoapHeaders($headers);

  return $client;
}

$client = createPWSSOAPClient();
$request->PINS = array();
$request->PINs->PIN->Value = "1234567";
$response = $client->TrackPackagesByPin($request);

The request itself should look like the following:


/** 
  * SOAP Request Envelope (Request Made from the SOAP Client)
  * <?xml version="1.0" encoding="UTF-8"?>
  * <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://example.com/datatypes/v1"><SOAP-ENV:Header><ns1:RequestContext><ns1:Version>1.0</ns1:Version><ns1:Language>en</ns1:Language><ns1:GroupID>xxx</ns1:GroupID><ns1:RequestReference>Rating Example</ns1:RequestReference></ns1:RequestContext></SOAP-ENV:Header><SOAP-ENV:Body><ns1:TrackByPIN><ns1:PINs><ns1:PIN><ns1:Value>1234567</ns1:Value></ns1:PIN></ns1:PINs></ns1:TrackByPIN></SOAP-ENV:Body></SOAP-ENV:Envelope>
**/

However, when I run the above code I get: PHP Fatal error: Uncaught Error: Attempt to assign property "PINS" on null I realize that request is null but I don't know what to set it to because I do need the SOAP-ENV:Body but couldn't find anything in the SOAP docs about setting a body.

Nothingbetter
  • 48
  • 2
  • 14

1 Answers1

3

This issue is related to well documented Backward Incompatible Changes in PHP 8 (see here).

Basically, PHP < 7.4 allowed you to assign properties and create an object on the fly. PHP 8 does not allow this anymore (i.e. because the object doesn't exist, something you recognize). Specifically:

  • A number of warnings have been converted into Error exceptions:
    • Attempting to write to a property of a non-object. Previously this implicitly created an stdClass object for null, false and empty strings. (reference)

To resolve, set the properties of an object on creation, rather than referencing new properties of something that is null and is not yet created. See here for reference:

$client = createPWSSOAPClient();

$request = (object)[
     'PINS' => (object)[
          'PIN' => (object)[
               'Value' => "1234567"      
           ],
     ],
 ];

$response = $client->TrackPackagesByPin($request);
Etienne Jacquot
  • 176
  • 1
  • 9