PHP 使用安全浏览 API 检查网站 url
PHP check website url using safe browsing API
我正在尝试使用 Google 安全浏览 API
检查 url 是否安全
我遵循了以下教程
https://developers.google.com/safe-browsing/v4/get-started
我已经按照上面的步骤完成了下面的步骤url指南
- 获取帐户
- 创建项目
- 设置 API 密钥
最后我得到了
https://safebrowsing.googleapis.com/v4/...?key=API_KEY
我的问题是如何将网站 url 传递到上面的安全浏览 url
post 请求的请求正文如下所示:
{
"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
部分发送 url。但是您传递的是 Google url(http://www.google.com),而是添加您需要检查的 urls。
不言自明,参见:url 至 check1.org ("http://www.urltocheck1.org/"
)
完成以下步骤以启用 API 并获取 API 密钥:
- 打开Google Developers Console API Library.
- 从项目下拉列表中,select一个项目或创建一个新项目。
- 在 Google APIs 选项卡中,搜索 select 安全浏览 API,然后点击启用API。
- 接下来,在左侧边栏中 select Credentials.
- Select Create credentials 下拉菜单,然后选择 API key.
- 根据您的应用程序,从创建新密钥弹出窗口,select 浏览器密钥或服务器密钥 .
- 输入密钥的名称,设置可选的引荐来源网址或 IP 地址,然后单击 创建。您的密钥已创建并显示在弹出窗口 window 中。密钥也列在凭据页面上。
这是 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://whosebug.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://whosebug.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://whosebug.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;
}
我正在尝试使用 Google 安全浏览 API
检查 url 是否安全我遵循了以下教程
https://developers.google.com/safe-browsing/v4/get-started
我已经按照上面的步骤完成了下面的步骤url指南
- 获取帐户
- 创建项目
- 设置 API 密钥
最后我得到了 https://safebrowsing.googleapis.com/v4/...?key=API_KEY
我的问题是如何将网站 url 传递到上面的安全浏览 url
post 请求的请求正文如下所示:
{
"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
部分发送 url。但是您传递的是 Google url(http://www.google.com),而是添加您需要检查的 urls。
不言自明,参见:url 至 check1.org ("http://www.urltocheck1.org/"
)
完成以下步骤以启用 API 并获取 API 密钥:
- 打开Google Developers Console API Library.
- 从项目下拉列表中,select一个项目或创建一个新项目。
- 在 Google APIs 选项卡中,搜索 select 安全浏览 API,然后点击启用API。
- 接下来,在左侧边栏中 select Credentials.
- Select Create credentials 下拉菜单,然后选择 API key.
- 根据您的应用程序,从创建新密钥弹出窗口,select 浏览器密钥或服务器密钥 .
- 输入密钥的名称,设置可选的引荐来源网址或 IP 地址,然后单击 创建。您的密钥已创建并显示在弹出窗口 window 中。密钥也列在凭据页面上。
这是 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://whosebug.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://whosebug.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://whosebug.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;
}