php - 通过 PHP 从 URL 读取我是否被 PWNED JSON 值
问题描述
我正在尝试使用“ Have I Been PWNED ”API v2读取 JSON 值。
我尝试了两种不同的方法来使用 URL ( https://haveibeenpwned.com/api/v2/breach/Adobe ) 和使用本地 .json 文件来显示数据,但这两种方法都没有显示任何内容。
方法一(网址):
索引.php
<?php
// Breach Title
$breach_title = 'Adobe';
// JSON URL
$url = 'https://haveibeenpwned.com/api/v2/breach/'.$breach_title;
// Put the contents of the url into a variable
$json = file_get_contents($url);
// Decode the JSON feed
$object = json_decode($json);
// Echo Results
echo $object[0]->Title;
echo $object[0]->Name;
echo $object[0]->Domain;
echo $object[0]->Description;
echo $object[0]->BreachDate;
?>
方法 2(本地 .json 文件):
索引.php
<?php
// Put the contents of the file into a variable
$json = file_get_contents("Adobe.json");
// Decode the JSON feed
$object = json_decode($json);
// Echo Results
echo $object[0]->Title;
echo $object[0]->Name;
echo $object[0]->Domain;
echo $object[0]->Description;
echo $object[0]->BreachDate;
?>
Adobe.json
{
"Title": "Adobe",
"Name": "Adobe",
"Domain": "adobe.com",
"BreachDate": "2013-10-04",
"AddedDate": "2013-12-04T00:00:00Z",
"ModifiedDate": "2013-12-04T00:00:00Z",
"PwnCount": 152445165,
"Description": "In October 2013, 153 million Adobe accounts were breached with each containing an internal ID, username, email, <em>encrypted</em> password and a password hint in plain text. The password cryptography was poorly done and <a href=\"http://stricture-group.com/files/adobe-top100.txt\" target=\"_blank\" rel=\"noopener\">many were quickly resolved back to plain text</a>. The unencrypted hints also <a href=\"http://www.troyhunt.com/2013/11/adobe-credentials-and-serious.html\" target=\"_blank\" rel=\"noopener\">disclosed much about the passwords</a> adding further to the risk that hundreds of millions of Adobe customers already faced.",
"DataClasses": [
"Email addresses",
"Password hints",
"Passwords",
"Usernames"
],
"IsVerified": true,
"IsFabricated": false,
"IsSensitive": false,
"IsActive": true,
"IsRetired": false,
"IsSpamList": false,
"LogoType": "svg"
}
我一直在使用以下资源:
在这两种方法中也没有输出:
print_r($object);
解决方案
使用您问题中的 Adobe.json 版本,您不需要[0]
访问数据...
$object = json_decode($json);
// Echo Results
echo $object->Title;
echo $object->Name;
echo $object->Domain;
echo $object->Description;
echo $object->BreachDate;
要使用 https 获取文件 URL,使用 CURL 可能更容易...
// Breach Title
$breach_title = 'Adobe';
// JSON URL
$url = 'https://haveibeenpwned.com/api/v2/breach/'.$breach_title;
// Put the contents of the url into a variable
//$json = file_get_contents($url);
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false // Disabled SSL Cert checks
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$json = curl_exec( $ch );
// Decode the JSON feed
$object = json_decode($json);
$object = json_decode($json);
// Echo Results
echo $object->Title;
echo $object->Name;
echo $object->Domain;
echo $object->Description;
echo $object->BreachDate;
推荐阅读
- python - 将 hh:mm:ss 转换为浮点数以进行进一步计算
- ios - 如何在 Mapbox SDK 上检查用户是否已到达目的地?
- database - 如何在 dot Net 中连接 Gremlin?
- r-exams - 当 exshuffle 开启时,如何更改问题的正确答案并从头开始复制考试(包“考试”)?
- vb.net - 检查整数是否是有效的按位枚举值
- performance - 谷歌标签管理器导致页面加载延迟
- php - 凭据与我们的记录不符
- google-api - 使用 Google People API 未显示 Google 联系人
- javascript - SVG & JS - 光线被障碍物阻挡
- c# - 创建/更新和读取子实体 ASP.NET Web API