Google安全浏览API没有响应 - 发回响应状态

时间:2017-02-20 15:44:39

标签: php json curl safe-browsing

我正在尝试将Google安全浏览API集成到我的网站。 documentation

我尝试过:

$apiKey = 'MyFakeAPiKey-pozpkefpoqskpfazejsqpi';
$url = 'http://www.bbc.com/';
$url = urlencode($url);
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://sb-ssl.google.com/safebrowsing/api/lookup?client=firefox&key='. $apiKey.'&appver=1.5.2&pver=4.0&url='.$url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Content-length: auto";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

echo print_r($result, true);

我没有回应。我做错了什么?

2 个答案:

答案 0 :(得分:0)

我找到了解决方案。一切都按预期工作。

  $url = 'http://911.sos-empleados.net';


                  $apiKey = 'Your API';
                  $apiUrl = 'https://safebrowsing.googleapis.com/v4/threatMatches:find?key='.$apiKey;

                  $params = [
                      'client' => [
                          'clientId' => 'foobar',
                          'clientVersion' => '1.2.3'
                      ],
                      'threatInfo' => [
                             "threatTypes" =>["MALWARE", "SOCIAL_ENGINEERING"],
                            "platformTypes" => ["WINDOWS"],
                             'threatEntryTypes' => ['URL'],
                          'threatEntries' => [
                              [ 'url' => $url ]
                          ]
                      ]
                  ];


                  $ch = curl_init($apiUrl);
                  curl_setopt_array($ch, [
                      CURLOPT_POST => 1,
                      CURLOPT_RETURNTRANSFER => 1,
                      CURLOPT_HEADER => 1,
                      CURLOPT_POSTFIELDS => json_encode($params),
                      CURLOPT_HTTPHEADER => [
                          'Content-Type: text/json'
                      ]
                  ]);

                  $res = curl_exec($ch);
                  ?> <pre><?php echo print_r($res, true); ?></pre> <?php
                  ?>

答案 1 :(得分:0)

您使用的是旧版API。代码中的端点对应于安全浏览API的版本1,该版本于2011年12月已弃用,并且已关闭。

您需要更新到支持的API版本 - 最好是v4,因为它是最新版本。