2017-06-15 163 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
' –