【问题标题】:WHOIS API JSON Array ContentWHOIS API JSON 数组内容
【发布时间】:2017-11-17 17:45:47
【问题描述】:

我需要一些帮助。正如您所想象的那样,由于我已经在这方面搞砸了将近五个小时,现在我有点沮丧。

我的挫败感主要是由于我对 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);
        }
    }
  }

?>

当我执行它时,我立即被以下错误猛烈抨击,当某些数据丢失(例如注册人的姓名)时,错误数量会增加。


注意:未定义属性:stdClass::$ErrorMessage in /home/users/pcsnlftp/india.pcs-nl.com/includes/scripts/test/test-processor.php 上线47
域名:google.com
创建日期: 1997-09-15T00:00:00-0700
更新日期: 2015-06-12T10:38:52-0700
注册人:
注册人姓名:Dns 管理员注册人组织:Google Inc. 注册人街道:请 联系contact-admin@google.com,1600 Amphitheatre Parkway Registrant 城市:山景城 注册人州/省:CA Registrant Postal 代码:94043 注册人国家:美国注册人电话:+1.6502530000 注册人传真:+1.6506188571 注册人电子邮件:dns-admin@google.com

我的问题是两方面的;

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

任何帮助将不胜感激!

【问题讨论】:

  • if(isset($res-&gt;ErrorMessage)) 应该解决您的第一个问题....第二个只需使用例如$domain=$whoisRecord-&gt;domainName 而不是整个回声
  • 非常感谢您的澄清,请原谅我对此的无知;但如果它像$domain=$whoisRecord-&gt;domainName 一样简单,他们为什么要使用print_r
  • print_r($variable,1) 返回值....更多详细信息在php.net/manual/en/function.print-r.php

标签: php mysql arrays json api


【解决方案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>";
        }
    }
  }

?>

【讨论】:

  • 非常感谢您的回答,我会在几个小时内尝试代码并报告!
【解决方案2】:

使用它从 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'];

【讨论】:

  • 非常感谢您的回答,我会在几个小时内尝试代码并报告!这是我尚未尝试过的获取数据的一种变体。
  • 该死,当使用以下代码时; $res=json_decode($contents); if($res){ $whoisRecord = $res-&gt;WhoisRecord; if($whoisRecord){ echo "Name".$res['WhoisRecord']['registrant']['name']; } } 我收到以下错误:Cannot use object of type stdClass as array in &lt;b&gt;/home/users/pcsnlftp/india.pcs-nl.com/includes/scripts/test/test-processor.php&lt;/b&gt; on line &lt;b&gt;49&lt;/b&gt;&lt;br /&gt;
  • ,true 添加到json_decode 时出现以下错误:&lt;b&gt;Notice&lt;/b&gt;: Trying to get property of non-object in &lt;b&gt;/home/users/pcsnlftp/india.pcs-nl.com/includes/scripts/test/test-processor.php&lt;/b&gt; on line &lt;b&gt;47&lt;/b&gt;&lt;br /&gt;
猜你喜欢
  • 1970-01-01
  • 2015-12-19
  • 1970-01-01
  • 2013-11-10
  • 2014-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多