我有一个 php 脚本,它尝试使用 google Safe Browsing Lookup 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 数组格式是否有问题或可能是什么问题?