Php 谁是API JSON数组内容
我需要一些帮助。正如你可能想象的那样,我已经在这件事上闹了将近五个小时了,现在我有点沮丧了 我的沮丧主要是因为我对JSON数组,尤其是更复杂的数组以及如何在PHP中处理它们缺乏经验。到目前为止,无论我如何修改下面的示例代码,都会导致大量错误或没有执行任何脚本 我使用以下服务->查找与某个域/IP有关的数据 该服务返回一个JSON数组,例如:`Php 谁是API JSON数组内容,php,mysql,arrays,json,api,Php,Mysql,Arrays,Json,Api,我需要一些帮助。正如你可能想象的那样,我已经在这件事上闹了将近五个小时了,现在我有点沮丧了 我的沮丧主要是因为我对JSON数组,尤其是更复杂的数组以及如何在PHP中处理它们缺乏经验。到目前为止,无论我如何修改下面的示例代码,都会导致大量错误或没有执行任何脚本 我使用以下服务->查找与某个域/IP有关的数据 该服务返回一个JSON数组,例如:` { "WhoisRecord": { "createdDate": "1997-09-15T00:00:00-0700", "upda
{
"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);
}
}
}
?>
ErrorMessage){
echo$res->ErrorMessage->msg;
}
否则{
$whoisRecord=$res->whoisRecord;
如果($whoisRecord){
echo“域名:”.print\r($whoisRecord->domainName,1)。“
”;
echo“创建日期:”.print_r($whoisRecord->createdDate,1)。“
”;
echo“更新日期:”.print_r($whoisRecord->updatedate,1)。“
”;
如果($whoisRecord->注册人)回显“注册人:
”。打印($whoisRecord->注册人->原始文本,1)。”;
echo "Name".$res['WhoisRecord']['registrant']['name'];
echo "Organization".$res['WhoisRecord']['registrant']['organization'];
//打印(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
echo "Name".$res['WhoisRecord']['registrant']['name'];
echo "Organization".$res['WhoisRecord']['registrant']['organization'];
管理注册人组织:谷歌公司注册人街道:请
接触-admin@google.com,1600圆形剧场公园道注册人
城市:山景城注册人州/省:CA注册人邮政
代码:94043注册国:美国注册人电话:+1.650253000
注册人传真:+1.6506188571注册人电子邮件:dns-admin@google.com
我的问题是双管齐下的
任何帮助都将不胜感激 用于从json数组获取数据
echo "Name".$res['WhoisRecord']['registrant']['name'];
echo "Organization".$res['WhoisRecord']['registrant']['organization'];
如果它不工作,那么首先$res=json\u decode($contents)
到$res=json\u解码($contents,true)代码>然后使用此
<?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>";
}
}
}
?>
这应该做到:
if(isset($res->ErrorMessage))
应该解决您的第一个问题。。。。第二,只需使用例如$domain=$whoisRecord->domainName
而不是整个echo内容感谢您的澄清,请原谅我对此一无所知;但是如果它像$domain=$whoisRecord->domainName
那么简单,为什么他们要使用print\r
?print\r($variable,1)返回值。。。。更多详细信息,非常感谢您的回答,我将在几个小时内尝试代码并报告!非常感谢您的回答,我将在几个小时内尝试代码并报告!这是我还没有尝试过的获取数据的变体<代码>$res=json\u解码($contents);如果($res){$whoisRecord=$res->whoisRecord;如果($whoisRecord){echo“Name”。$res['whoisRecord']['registent']['Name'];}}
我得到以下错误:添加时,无法在第49行的/home/users/pcsnlftp/india.pcs-nl.com/includes/scripts/test/test-processor.php中将stdClass类型的对象用作数组,true
对于json_解码,我得到以下错误:注意:试图在第47行的/home/users/pcsnlftp/india.pcs-nl.com/includes/scripts/test/test-processor.php中获取非对象的属性