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

Safe Browsing Lookup API (v4) Invalid JSON payload received

我有一个 php 脚本试图使用 google 安全浏览查找 API (v4),但我收到错误 "Invalid JSON payload received. Unknown name \"\": Root元素必须是消息..."

这是我的代码:

<?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 数组格式是否有问题或者可能是什么问题?

您 运行 json_encode 使用的数据已经编码。

即。更改此行:

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

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