0

I have a php script which tries to use google Safe Browsing Lookup API (v4), but I'm getting error "Invalid JSON payload received. Unknown name \"\": Root element must be a message..."

Here is my code:

<?php

$data = '{
  "client": {
    "clientId": "TestClient",
    "clientVersion": "1.0"
  },
  "threatInfo": {
    "threatTypes":      ["MALWARE", "SOCIAL_ENGINEERING"],
    "platformTypes":    ["LINUX"],
    "threatEntryTypes": ["URL"],
    "threatEntries": [
      {"url": "http://www.google.com"}
    ]
  }
}';


$apikey = "my_secret_api_key";
$url_send ="https://safebrowsing.googleapis.com/v4/threatMatches:find?key=".$apikey."";

$str_data = json_encode($data);

function sendPostData($url, $post){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", 'Content-Length: ' . strlen($post)));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
return $result;
}

$jaahas = sendPostData($url_send, $str_data);

echo "<pre>";
var_dump($jaahas);

?>

Is there something wrong with the json-data array formatting or what might be the problem?

josu
  • 3
  • 3

1 Answers1

1

You're running json_encode on data which is already encoded.

ie. change this line:

$jaahas = sendPostData($url_send, $str_data);

to

$jaahas = sendPostData($url_send, $data);
Vortigern
  • 66
  • 5
  • Thank you, but when I change that, I get only answer: **string(3) "{}"**. And according to Google documentation the response should be something else: https://developers.google.com/safe-browsing/v4/lookup-api#http-post-response – josu May 25 '16 at 07:17
  • 1
    Actually problem is solved. The code works with that litle modification by @Vortigern Google just does not give any response if the site is not infected! – josu May 25 '16 at 11:03