尝试为 null 分配属性

发布于 2025-01-10 04:01:24 字数 2773 浏览 0 评论 0原文

我正在尝试在 PHP 中创建一个肥皂请求,并且对两者都是新手。我有:

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

请求本身应该如下所示:


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

但是,当我运行上面的代码时,我得到: PHP 致命错误:未捕获错误:尝试在 null 上分配属性“PINS” 我意识到该请求为空,但我不知道将其设置为什么,因为我确实需要 SOAP-ENV:Body 但在 SOAP 文档中找不到有关设置主体的任何内容。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

菩提树下叶撕阳。 2025-01-17 04:01:24

此问题与有据可查的PHP 8 中的向后不兼容更改有关(请参阅此处)。

基本上,PHP < 7.4 允许您动态分配属性并创建对象。 PHP 8 不再允许这样做(即因为该对象不存在,您认识的东西)。具体来说:

  • 许多警告已转换为错误异常:
    • 尝试写入非对象的属性。以前,这会为 null、false 和空字符串隐式创建 stdClass 对象。 (参考)

要解决此问题,请在创建时设置对象的属性,而不是引用 null 且尚未创建的对象的新属性。请参阅此处以供参考:

$client = createPWSSOAPClient();

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

$response = $client->TrackPackagesByPin($request);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文