【发布时间】: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
我的问题是两方面的;
- 我如何摆脱基本上没有用(对我而言)的错误?
- 如何将所需的数据放入我可以插入到 MySQL 数据库中的变量中?
任何帮助将不胜感激!
【问题讨论】:
-
if(isset($res->ErrorMessage))应该解决您的第一个问题....第二个只需使用例如$domain=$whoisRecord->domainName而不是整个回声 -
非常感谢您的澄清,请原谅我对此的无知;但如果它像
$domain=$whoisRecord->domainName一样简单,他们为什么要使用print_r? -
print_r($variable,1) 返回值....更多详细信息在php.net/manual/en/function.print-r.php