通过 PHP 从 URL 中读取我是否已 PWNED JSON 值 [英] Reading Have I Been PWNED JSON values from URL via PHP

查看:32
本文介绍了通过 PHP 从 URL 中读取我是否已 PWNED JSON 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 "我是否被 PWNED" API 读取 JSON 值v2.

I am trying to read JSON values using the "Have I Been PWNED" API v2.

我尝试了两种不同的方式来使用 URL (https://haveibeenpwned.com/api/v2/breach/Adobe) 并使用本地 .json 文件,但两种方法均未显示任何内容.

I have tried two different ways to display the data using the URL (https://haveibeenpwned.com/api/v2/breach/Adobe) and using a local .json file, but both methods display nothing.

方法 1(网址):

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;
?>

方法 2(本地 .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"
}

我一直在使用以下资源:

I have been using the following as resources:

在两种方法中也没有使用它的输出:

Also no output using this in both methods:

print_r($object);

推荐答案

这是我在仔细阅读 HIBP APIv2 后更正的代码,感谢@MonkeyZeus

Here is my corrected code after reading the HIBP APIv2 closer thanks to @MonkeyZeus

只需要添加一个用户代理并删除 [0].

Just needed to add a user agent and removed [0].

<?php
    ini_set('user_agent', 'Test App');

    // 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->Title;
    echo $object->Name;
    echo $object->Domain;
    echo $object->Description;
    echo $object->BreachDate;
?>

这篇关于通过 PHP 从 URL 中读取我是否已 PWNED JSON 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆