【发布时间】:2018-12-01 02:40:22
【问题描述】:
我正在尝试使用 "Have I Been PWNED" API v2 读取 JSON 值。
我尝试了两种不同的方法来使用 URL (https://haveibeenpwned.com/api/v2/breach/Adobe) 和使用本地 .json 文件来显示数据,但两种方法都没有显示任何内容。
方法一(网址):
index.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;
?>
方法二(本地.json文件):
index.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"
}
我一直在使用以下资源:
- Read JSON values from URL via PHP
- How do I extract data from JSON with PHP?
- https://stackoverflow.com/a/29308899/9925901
在这两种方法中也没有输出:
print_r($object);
【问题讨论】:
-
打开
<?php后把这些:ini_set('display_errors', 1); error_reporting(E_ALL);再试一次。 -
我收到
Warning: file_get_contents(https://haveibeenpwned.com/api/v2/breach/Adobe): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in /Applications/MAMP/htdocs/HIBP/index.php on line 9 -
var_dump()和print_r()非常有用。$object是一个对象,而不是您的示例中的对象数组。 -
感谢您链接他们的API docs,不要继续关注他们。您是否阅读了概述部分的第 2 步?
-
谢谢!知道这将是一件简单的事情。我删除了 [0] 并添加了一个用户代理。