通过PHP从URL读取PWNED JSON值

时间:2018-06-21 17:06:17

标签: php json url

我正在尝试使用"Have I Been PWNED" API v2读取JSON值。

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

方法1(URL):

index.php

def message_converter(message_id):
        message = service.users().messages().get(userId='me', id=message_id,format='raw').execute()
        msg_str = str(base64.urlsafe_b64decode(message['raw'].encode('ASCII')),'UTF-8')
        mime_msg = email.message_from_string(msg_str)
        if mime_msg.is_multipart():
            for payload in mime_msg.get_payload():
                # if payload.is_multipart(): ...
                print (payload.get_payload())
        else:
            print (mime_msg.get_payload())

方法2(本地.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;
?>

Adob​​e.json

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

我一直在使用以下资源:

在两种方法中也都没有使用此输出:

{
    "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"
}

2 个答案:

答案 0 :(得分:1)

问题中出现了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;

答案 1 :(得分:1)

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

只需添加一个用户代理并删除[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;
?>