duandoucou7200 2016-05-24 10:25
浏览 49
已采纳

安全浏览查找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..."

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?

  • 写回答

1条回答 默认 最新

  • doudeng1870 2016-05-24 17:56
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题