PHP如何提取Transip API返回Domain Object中受保护的name属性值
问题描述
我的域名注册商正在调整API,我正尝试实现DDNS文件的自动化更新。
通过API完成授权后我可以获取名下所有域名数据,返回结构如下:
Array ( [0] => Transip\Api\Library\Entity\Domain Object ( [name:protected] => myfirstdomain.org [authCode:protected] => xxxxxxxxxxxx [isTransferLocked:protected] => [registrationDate:protected] => 2020-01-17 [renewalDate:protected] => 2022-01-17 [isWhitelabel:protected] => [cancellationDate:protected] => [cancellationStatus:protected] => [isDnsOnly:protected] => [hasAutoDns:protected] => [tags:protected] => Array ( ) ) [1] => Transip\Api\Library\Entity\Domain Object ( [name:protected] => myseconddomain.org [authCode:protected] => xxxxxxxxxx [isTransferLocked:protected] => [registrationDate:protected] => 2009-03-22 [renewalDate:protected] => 2022-03-22 [isWhitelabel:protected] => [cancellationDate:protected] => [cancellationStatus:protected] => [isDnsOnly:protected] => [hasAutoDns:protected] => [tags:protected] => Array ( ) ) [2] => Transip\Api\Library\Entity\Domain Object ( [name:protected] => mythirddomain.org [authCode:protected] => xxxxxxxxxxxxx [isTransferLocked:protected] => [registrationDate:protected] => 2011-09-17 [renewalDate:protected] => 2022-09-17 [isWhitelabel:protected] => [cancellationDate:protected] => [cancellationStatus:protected] => [isDnsOnly:protected] => [hasAutoDns:protected] => [tags:protected] => Array ( ) ) )
我编写的提取域名的代码如下:
//get all domain information $domains = $api->domains()->getAll(); //this pulls all the first domain data $firstDomain = $domains[0]; //Trying to strip the [name] value $firstDomainName = $firstDomain->name;
运行后触发报错:
PHP Fatal error: Uncaught Error: Cannot access protected property Transip\Api\Library\Entity\Domain::$name in /myserver/websitefolder/Check.php:22
我对$domains执行json_encode操作的返回结果如下:
[{"name":"myfirstdomain.org","authCode":"xxxxxxxxxx","isTransferLocked":false,"registrationDate":"2020-01-17","renewalDate":"2022-01-17","isWhitelabel":false,"cancellationDate":"","cancellationStatus":"","isDnsOnly":false,"hasAutoDns":false,"tags":[]},{"name":"myseconddomain.org","authCode":"xxxxxxxxxx","isTransferLocked":false,"registrationDate":"2009-03-22","renewalDate":"2022-03-22","isWhitelabel":false,"cancellationDate":"","cancellationStatus":"","isDnsOnly":false,"hasAutoDns":false,"tags":[]},{"name":"mythirddomain.org","authCode":"xxxxxxxxxxxxx","isTransferLocked":false,"registrationDate":"2011-09-17","renewalDate":"2022-09-17","isWhitelabel":false,"cancellationDate":"","cancellationStatus":"","isDnsOnly":false,"hasAutoDns":false,"tags":[]}]
请问我该如何正确提取所有域名的name值?
解决方案
报错的核心原因是Transip\Api\Library\Entity\Domain类的$name属性是受保护(protected)类型,PHP的访问控制规则不允许外部直接访问类的protected/private属性。
方案1:使用官方实体类的Getter方法(推荐)
这类商用SDK的实体类都会为受保护属性提供公开的取值方法,你可以直接调用getName()方法获取域名值,批量提取的代码如下:
$domains = $api->domains()->getAll(); $domainNames = []; foreach ($domains as $domain) { $domainNames[] = $domain->getName(); } // 输出所有域名验证 print_r($domainNames);
方案2:借助JSON序列化转数组提取
你已经验证该类可以被正常JSON序列化输出name字段,也可以通过序列化+反序列化转成数组后批量提取:
$domains = $api->domains()->getAll(); // 第二个参数传true表示转成关联数组 $domainArray = json_decode(json_encode($domains), true); // 直接提取所有name列 $domainNames = array_column($domainArray, 'name'); print_r($domainNames);
内容的提问来源于stack exchange,提问作者Jacco
相关产品推荐
相关产品推荐

