尝试为 null 分配属性
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此问题与有据可查的PHP 8 中的向后不兼容更改有关(请参阅此处)。
基本上,PHP < 7.4 允许您动态分配属性并创建对象。 PHP 8 不再允许这样做(即因为该对象不存在,您认识的东西)。具体来说:
要解决此问题,请在创建时设置对象的属性,而不是引用 null 且尚未创建的对象的新属性。请参阅此处以供参考:
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:
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: