安全浏览查找API(v4)收到无效的JSON有效负载

时间:2016-05-24 10:25:54

标签: php api curl safe-browsing

我有一个php脚本试图使用谷歌安全浏览查找API(v4),但我收到错误“收到无效的JSON有效负载。未知名称\”\“:根元素必须是一条消息...”

这是我的代码:

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

?>

json-data数组格式是否有问题或可能是什么问题?

1 个答案:

答案 0 :(得分:1)

您正在对已编码的数据运行json_encode。

即。改变这一行:

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

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