Dr. House
Dr. House

Reputation: 562

Google Safe browsing API is not responding - sending back response status

I am trying to integrate Google Safe browsing API to my website. The documentation:

What I have tried:

$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);

I get no response.What I am doing wrong?

Upvotes: 0

Views: 986

Answers (3)

Levitskyi
Levitskyi

Reputation: 1

An empty response is normal for this API.

API version 4 returns an empty object if the url is not found in the database:

{}

If the url is found in the database, it returns an object like this:

{
    "matches": [{
        "threatType": "MALWARE",
        "platformType": "ALL_PLATFORMS",
        "threat": {
            "url": "http://malware.testing.google.test/testing/malware/"
        },
        "cacheDuration": "300s",
        "threatEntryType": "URL"
    }]
}

You can use this url to check: http://malware.testing.google.test/testing/malware/

Upvotes: 0

user149341
user149341

Reputation:

You're using an old version of the API. The endpoint in your code corresponds to version 1 of the Safe Browsing API, which was deprecated in December 2011, and which has since been shut down.

You will need to update to a supported version of the API -- preferably v4, as that's current.

Upvotes: 0

Dr. House
Dr. House

Reputation: 562

I found a solution. Everything works as expected.

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

Upvotes: 0

Related Questions