my registrar is changing API and I am trying to automate my DDNS file.
Via the API I can authorize and get all my domain data:
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
(
)
)
)
Now I want to just extract the domain names to use in a update script. Unfortunatly I can't seem to manage it:
//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;
The last line unfortunately kicks a error
PHP Fatal error: Uncaught Error: Cannot access protected property Transip\Api\Library\Entity\Domain::$name in /myserver/websitefolder/Check.php:22
If I 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":[]}]
CodePudding user response:
PHP doesn't like directly accessing protected values, instead you use the "getter" method. Conversely, if you want to set the value of a protected field you use the "setter" method. It was a lucky (but informed) guess on my part that the getter method for the "name" field was getName()
. You can check the class definition to see which methods are available to you.
Honestly I can't say for sure when exactly you should enforce using getter/setter methods over allowing direct access to elements when defining your own class, but I am certain that using the search terms "getter setter method" will show up some helpful information.
CodePudding user response:
Look at the class definition of Transip\Api\Library\Entity\Domain Object
. It should have a method to allow you access to the domain name. The field name
is protected so it's not accessible to you unless you are extending the class.
CodePudding user response:
Now that the solution to pull the name is there the solution is:
<?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
}
?>
Thank you so much for the help