我的注冊商正在更改 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
如果我 json_encode $domains:
[{"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":[]}]
uj5u.com熱心網友回復:
PHP 不喜歡直接訪問受保護的值,而是使用“getter”方法。相反,如果要設定受保護欄位的值,請使用“setter”方法。我很幸運(但知情)猜測“名稱”欄位的 getter 方法是getName(). 您可以檢查類定義以查看哪些方法可供您使用。
老實說,我不能確定何時應該強制使用 getter/setter 方法,而不是在定義自己的類時允許直接訪問元素,但我確信使用搜索詞“getter setter 方法”會顯示一些有用的資訊.
uj5u.com熱心網友回復:
查看 的類定義Transip\Api\Library\Entity\Domain Object。它應該有一種方法可以讓您訪問域名。該欄位name是受保護的,因此除非您擴展該類,否則您無法訪問它。
uj5u.com熱心網友回復:
既然提取名稱的解決方案就在那里,解決方案是:
<?php
/**
* A script to update all the @ entries in my registrar
*/
require_once (__DIR__ . '/Authenticate.php');
//get all domain information
$domains = $api->domains()->getAll();
//get the current IP address
$ipAddress = file_get_contents('http://ipecho.net/plain');
//set the counter to 0 for first responds
$i = 0;
foreach ($domains as &$value){
$domain = $domains[$i];
$domainName = $domain->getName();
$i = $i 1;
echo $domainName;
// Get the DNS for the current Domain
$domainDns = $api->DomainDns()->getByDomainName($domainName);
$domainIp = $domainDns[0];
$domainIp = $domainIp->getContent();
//compare the registrar IP to current IP
if($domainIp == $ipAddress) {
echo "IP unchanged";
} else {
//Here we update the record of the domain that needs changing
// Create a DNS entry object
$dnsEntry = new DnsEntry();
$dnsEntry->setName('@');
$dnsEntry->setExpire('300');
$dnsEntry->setType('A');
$dnsEntry->setContent($ipAddress);
// Apply entry
$api->domainDns()->updateEntry($domainName, $dnsEntry);
//end of else statement
}
//end of foreach
}
?>
十分感謝你的幫助
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/370971.html
