我需要一些帮助。正如您想象的那样,由于我已经为此花费了将近五个小时的时间,现在我有点沮丧。
我的沮丧主要是由于我对 JSON 数组缺乏经验,尤其是更复杂的数组以及如何在 PHP 中处理它们。到目前为止,无论我尝试修改下面的示例代码,都会导致大量错误或无法执行任何脚本...
我正在使用以下服务 -> WHOIS API用于查找与特定域/IP 相关的数据。
该服务返回一个 JSON 数组,例如:`
{
"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 contact-admin@google.com, 1600 Amphitheatre Parkway",
"city": "Mountain View",
"state": "CA",
"postalCode": "94043",
"country": "UNITED STATES",
"email": "dns-admin@google.com",
"telephone": "16502530000",
"fax": "16506188571",
"rawText": "Registrant Name: Dns Admin\nRegistrant Organization: Google Inc.\nRegistrant Street: Please contact contact-admin@google.com, 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: dns-admin@google.com"
},
"administrativeContact": {
"name": "DNS Admin",
"organization": "Google Inc.",
"street1": "1600 Amphitheatre Parkway",
"city": "Mountain View",
"state": "CA",
"postalCode": "94043",
"country": "UNITED STATES",
"email": "dns-admin@google.com",
"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: dns-admin@google.com"
}
}
}`
我感兴趣的是 WhoisRecord -> 注册人部分(姓名、组织、街道 1、城市、州、邮政编码、国家/地区、电子邮件等)
到目前为止,还不错。
但是,当我运行 PHP 代码示例时,他们提供的 API 内容开始让我有点困惑。代码如下所示:
<?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);
}
}
}
?>
当我执行它时,我立即遇到以下错误,当某些数据丢失时(例如注册人的姓名),错误数量会增加。
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 contact-admin@google.com, 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: dns-admin@google.com
我的问题有两个方面;
- 我如何摆脱那些(对我来说)基本上毫无用处的错误?
- 如何将所需数据放入变量中,例如,我可以将其插入 MySQL 数据库?
如有任何帮助,我们将不胜感激!
最佳答案
应该这样做:
<?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");
$res=json_decode($contents, true);
if($res){
if(isset($res['ErrorMessage'])){
echo $res['ErrorMessage'];
} else {
if(isset($res['WhoisRecord'])){
echo "Domain name: " . $res['WhoisRecord']['domainName']."<br/>";
echo "Created date: " .$res['WhoisRecord']['createdDate']."<br/>";
echo "Updated date: " .$res['WhoisRecord']['updatedDate']."<br/>";
if(isset($res['WhoisRecord']['registrant']))
echo "Registrant: <br/><pre>" . $res['WhoisRecord']['registrant']['rawText'] ."</pre>";
}
}
}
?>
关于php - WHOIS API JSON 数组内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44563988/