安全浏览查找API(v4)收到无效的JSON有效内容 [英] Safe Browsing Lookup API (v4) Invalid JSON payload received
本文介绍了安全浏览查找API(v4)收到无效的JSON有效内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个php脚本尝试使用谷歌安全浏览Lookup API(v4),但我收到错误收到无效的JSON有效负载未知名称\\:根元素必须是一条消息.. 。
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..."
这是我的代码:
<?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数据数组格式化有什么问题或可能是什么问题?
Is there something wrong with the json-data array formatting or what might be the problem?
推荐答案
您正在对已编码的数据运行json_encode。
You're running json_encode on data which is already encoded.
ie 。改变这一行:
ie. change this line:
$jaahas = sendPostData($url_send, $str_data);
到
$jaahas = sendPostData($url_send, $data);
这篇关于安全浏览查找API(v4)收到无效的JSON有效内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文