2017-06-15 168 views
1

我需要一些帮助。正如你所想象的那样,现在我已经在这个问题上打破了近五个小时,我感到有点沮丧。WHOIS API JSON数组内容

我的失望主要是由于我对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 [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]" 
    } 
    } 
}` 

所有我感兴趣的是WhoisRecord - >注册部分(姓名,单位,street1,城市,州,邮编,国家,电子邮件等)

到目前为止,这么好。

但是,当我运行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 [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]

我的问题是双管齐下的;

  1. 我该如何摆脱基本上无用的错误(对我)?
  2. 如何将所需的数据导入到我可以插入到MySQL数据库中的变量中?

任何帮助将不胜感激!

+0

'如果(isset($水库>的ErrorMessage ))'应该解决你的第一个问题....第二个只是使用例如'$ domain = $ whoisRecord-> domainName'而不是整个回声的东西 –

+0

非常感谢你澄清,请原谅我对此的无知;但如果它像'$ domain = $ whoisRecord-> domainName'那么简单,他们为什么使用'print_r'? –

+0

print_r($ variable,1)返回值....更多详细信息,请访问http://php.net/manual/en/function.print-r.php –

回答

1

这应做到:

<?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>"; 
     } 
    } 
    } 

?> 
+0

非常感谢您的回答,我会在几个小时内尝试一下代码并回报! –

+1

这一招做到了:) –

1

用于从json数组中获取数据。

echo "Name".$res['WhoisRecord']['registrant']['name']; 
echo "Organization".$res['WhoisRecord']['registrant']['organization']; 

如果它不工作,那么首先要$res=json_decode($contents);$res=json_decode($contents, true);然后用这个

echo "Name".$res['WhoisRecord']['registrant']['name']; 
echo "Organization".$res['WhoisRecord']['registrant']['organization']; 
+0

非常感谢您的回答,我会尝试在几个小时内完成代码并回报!这是我尚未尝试过的获取数据的变体。 –

+0

该死的,使用下面的代码时; '$ res = json_decode($ contents); if($ res){ \t \t $ whoisRecord = $ res-> WhoisRecord; 。 \t \t如果($ whoisRecord){ \t \t \t回声 “名称” $ RES [ 'WhoisRecord'] [ '注册'] [ '名称']; \t \t} \t}' 我得到以下错误: '不能在/home/users/pcsnlftp/india.pcs-nl.com/includes/scripts/test/test使用类型为stdClass的对象为数组-processor.php在线
' –

+0

当添加',TRUE'到json_decode我得到以下错误:'注意:试图让非对象的财产/家庭/用户/ pcsnlftp/india.pcs-nl.com/includes/scripts/test/test-processor.php on line
' –