【问题标题】:PHP check website url using safe browsing APIPHP 使用安全浏览 API 检查网站 url
【发布时间】:2016-12-21 09:31:16
【问题描述】:

我正在尝试使用 Google 安全浏览 API 检查网址是否安全

我按照以下教程进行操作

https://developers.google.com/safe-browsing/v4/get-started

我已按照上述网址指南完成以下步骤

  1. 获取帐户
  2. 创建项目
  3. 设置 API 密钥

最后我得到了 https://safebrowsing.googleapis.com/v4/...?key=API_KEY

我的问题是如何将网站 url 传递到上述安全浏览 url

【问题讨论】:

标签: php laravel codeigniter safe-browsing


【解决方案1】:

完成以下步骤以启用 API 并获取 API 密钥:

  1. 打开Google Developers Console API Library.
  2. 从项目下拉列表中,选择一个项目或创建一个新项目。
  3. Google APIs 标签中,搜索并选择Safe Browsing API,然后点击Enable API
  4. 接下来,在左侧边栏中选择凭据
  5. 选择Create credentials下拉菜单,然后选择API key
  6. 根据您的应用程序,从“创建新密钥”弹出窗口中选择浏览器密钥服务器密钥
  7. 输入密钥名称,设置可选的引荐来源网址或 IP 地址,然后点击创建。您的密钥已创建并显示在弹出窗口中。密钥也列在凭据页面上。

这是 100% 工作的 cURL 代码

curl -X POST \
  'https://safebrowsing.googleapis.com/v4/threatMatches:find?key=YOUR_KEY_HERE' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -H 'postman-token: ec7ffd77-d3fa-f017-44f9-d895efaba258' \
  -d '  {
    "client": {
      "clientId":      "yourcompanyname",
      "clientVersion": "1.5.2"
    },
    "threatInfo": {
      "threatTypes":      ["MALWARE", "SOCIAL_ENGINEERING"],
      "platformTypes":    ["WINDOWS"],
      "threatEntryTypes": ["URL"],
      "threatEntries": [
        {"url": "https://accounts-wallets.redirectme.net/webapps/57a39/websrc"},
        {"url": "http://stackoverflow.com/"}


      ]
    }
  }'

我得到了输出

{
    "matches": [
        {
            "threatType": "SOCIAL_ENGINEERING",
            "platformType": "WINDOWS",
            "threat": {
                "url": "https://accounts-wallets.redirectme.net/webapps/57a39/websrc"
            },
            "cacheDuration": "300s",
            "threatEntryType": "URL"
        }
    ]
}

Python 示例

import requests

url = "https://safebrowsing.googleapis.com/v4/threatMatches:find"

querystring = {"key":"YOUR_KEY"}

payload = """

  {
    "client": {
      "clientId":      "yourcompanyname",
      "clientVersion": "1.5.2"
    },
    "threatInfo": {
      "threatTypes":      ["MALWARE", "SOCIAL_ENGINEERING"],
      "platformTypes":    ["WINDOWS"],
      "threatEntryTypes": ["URL"],
      "threatEntries": [
        {"url": "https://accounts-wallets.redirectme.net/webapps/57a39/websrc"},
        {"url": "http://stackoverflow.com/"}


      ]
    }
  }

"""
headers = {
    'content-type': "application/json",
    'cache-control': "no-cache",
    'postman-token': "460d6d44-4a55-d6ab-fb58-2ff9fb306154"
    }

response = requests.request("POST", url, data=payload, headers=headers, params=querystring)

print(response.text)

PHP 示例

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://safebrowsing.googleapis.com/v4/threatMatches:find?key=YOUR_KEY",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => '  {
"client": {
  "clientId":      "yourcompanyname",
  "clientVersion": "1.5.2"
},
"threatInfo": {
  "threatTypes":      ["MALWARE", "SOCIAL_ENGINEERING"],
  "platformTypes":    ["WINDOWS"],
  "threatEntryTypes": ["URL"],
  "threatEntries": [
    {"url": "https://accounts-wallets.redirectme.net/webapps/57a39/websrc"},
    {"url": "http://stackoverflow.com/"}


  ]
}
  }',
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache",
    "content-type: application/json",
    "postman-token: b05b8d34-85f2-49cf-0f8e-03686a71e4e9"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

【讨论】:

  • 哇,这样的答案你得找小说了:)
【解决方案2】:

发布请求的请求正文如下所示:

{
    "client": {
      "clientId":      "yourcompanyname",
      "clientVersion": "1.5.2"
    },
    "threatInfo": {
      "threatTypes":      ["MALWARE", "SOCIAL_ENGINEERING"],
      "platformTypes":    ["WINDOWS"],
      "threatEntryTypes": ["URL"],
      "threatEntries": [
        {"url": "http://www.urltocheck1.org/"},
        {"url": "http://www.urltocheck2.org/"},
        {"url": "http://www.urltocheck3.com/"}
      ]
    }
}

如您所见,您需要在threatEntries 部分发送网址。但是您传递的是 Google url(http://www.google.com),而是添加您需要检查的 url。

不言自明,请参阅: url to check1.org ("http://www.urltocheck1.org/")

source

【讨论】:

  • @Raghavendra。感谢您的回答。我检查了我自己的网站网址,但它返回的是空 json。
  • 是的,如果在列表中找不到 url,它将返回空 json。请参阅注意:如果没有匹配项(即,如果在请求中指定的任何列表中都找不到请求中指定的 URL),则 HTTP POST 响应仅在响应正文中返回一个空对象
  • 到目前为止,此 URL 被标记为危险:activefile.ucoz.com。试试看是否有任何回应。
  • @Raghavendra.now 我得到 { "matches": [ { "threatType": "MALWARE", "platformType": "WINDOWS", "threat": { "url": "@987654323 @" }, "cacheDuration": "300s", "threatEntryType": "URL" } ] }
  • 在哪里可以找到“clientId”和“clientVersion”信息?因为当我创建项目时,它只给了我“项目名称”、“项目 ID”和“项目编号”
猜你喜欢
  • 2017-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-19
相关资源
最近更新 更多