I am in need of some help. As I have been breaking my balls on this for nearly five hours now I am getting a little frustrated, as you may imagine.
My frustration is mostly caused by my inexperience with JSON arrays, especially more complicated arrays and how to process them in PHP. Whatever I've tried so far to amend the example code below has resulted in a large number of errors or no script execution whatsoever...
I am using the following service -> WHOIS API for looking up data pertaining to a certain domain/IP.
The service returns a JSON array, like this one for example:`
{
"WhoisRecord": {
"createdDate": "1997-09-15T00:00:00-0700",
"updatedDate": "2015-06-12T10:38:52-0700",
"expiresDate": "2020-09-13T21:00:00-0700",
"registrant": {
"name": "Dns Admin",
"organization": "Google Inc.",
"street1": "Please contact [email protected], 1600 Amphitheatre Parkway",
"city": "Mountain View",
"state": "CA",
"postalCode": "94043",
"country": "UNITED STATES",
"email": "[email protected]",
"telephone": "16502530000",
"fax": "16506188571",
"rawText": "Registrant Name: Dns Admin\nRegistrant Organization: Google Inc.\nRegistrant Street: Please contact [email protected], 1600 Amphitheatre Parkway\nRegistrant City: Mountain View\nRegistrant State/Province: CA\nRegistrant Postal Code: 94043\nRegistrant Country: US\nRegistrant Phone: +1.6502530000\nRegistrant Fax: +1.6506188571\nRegistrant Email: [email protected]"
},
"administrativeContact": {
"name": "DNS Admin",
"organization": "Google Inc.",
"street1": "1600 Amphitheatre Parkway",
"city": "Mountain View",
"state": "CA",
"postalCode": "94043",
"country": "UNITED STATES",
"email": "[email protected]",
"telephone": "16506234000",
"fax": "16506188571",
"rawText": "Admin Name: DNS Admin\nAdmin Organization: Google Inc.\nAdmin Street: 1600 Amphitheatre Parkway\nAdmin City: Mountain View\nAdmin State/Province: CA\nAdmin Postal Code: 94043\nAdmin Country: US\nAdmin Phone: +1.6506234000\nAdmin Fax: +1.6506188571\nAdmin Email: [email protected]"
}
}
}`
All I am interested in is the WhoisRecord -> registrant part (name, organization, street1, city, state, postalcode, country, email, etc. etc.)
So far, so good.
However when I run the PHP code example they provide with their API stuff starts to become a bit confusing to me. The code is displayed below:
<?php
$username="YOUR_USERNAME";
$password="YOUR_PASSWORD";
$contents = file_get_contents("http://www.whoisxmlapi.com//whoisserver/WhoisService?domainName=google.com&username=$username&password=$password&outputFormat=JSON");
//echo $contents;
$res=json_decode($contents);
if($res){
if($res->ErrorMessage){
echo $res->ErrorMessage->msg;
}
else{
$whoisRecord = $res->WhoisRecord;
if($whoisRecord){
echo "Domain name: " . print_r($whoisRecord->domainName,1) ."<br/>";
echo "Created date: " .print_r($whoisRecord->createdDate,1) ."<br/>";
echo "Updated date: " .print_r($whoisRecord->updatedDate,1) ."<br/>";
if($whoisRecord->registrant)echo "Registrant: <br/><pre>" . print_r($whoisRecord->registrant->rawText, 1) ."</pre>";
//print_r($whoisRecord);
}
}
}
?>
I immediately get slammed with the following error when I execute it, the amount of errors increases when certain data is missing (for example the registrant's name).
Notice: Undefined property: stdClass::$ErrorMessage in /home/users/pcsnlftp/india.pcs-nl.com/includes/scripts/test/test-processor.php on line 47
Domain name: google.com
Created date: 1997-09-15T00:00:00-0700
Updated date: 2015-06-12T10:38:52-0700
Registrant:
Registrant Name: Dns Admin Registrant Organization: Google Inc. Registrant Street: Please contact [email protected], 1600 Amphitheatre Parkway Registrant City: Mountain View Registrant State/Province: CA Registrant Postal Code: 94043 Registrant Country: US Registrant Phone:+1.6502530000 Registrant Fax: +1.6506188571 Registrant Email: [email protected]
My question is two pronged;
- How do I get rid of the errors which basically serve no purpose (to me)?
- How do I get the required data into variables which I could, for example, insert into a MySQL database?
Any help would be greatly appreciated!
if(isset($res->ErrorMessage))
should solve your first question.... and for second just use for example$domain=$whoisRecord->domainName
instead of whole echo thing – undefined_variable$domain=$whoisRecord->domainName
why are they usingprint_r
? – Julian Koster