这是我的json数据
$data = array(
"api_key"=>"xxxx",
"email_details"=> array( "fromname"=>"test",
"subject"=>"Registration",
"from"=>"xxx@xxx.com",
"content"=>"test"),
"recipients"=>["xxx@gmail.com"]
);
生成json数据:
$str_data = json_encode($data);
给出如下格式的json:
{"api_key":"xxxx",
"email_details":{"fromname":"test","subject":"Registration","from":"xxx@xxx.com","content":"test"},
"recipients":["xxx@gmail.com"]}
是有效的json格式从这个url验证http://jsonlint.com/
我已经尝试运行API:https://api.xxx.com/xxx/json?data={"api_key":"xxxx","email_details": {"fromname":"xxx","subject":"Registration","from":"xxx@xxx.com","content":"test" },"recipients":["xxx@gmail.com"]}
得到成功消息
当我试图从PHP代码运行这个API时:
$url_email="https://api.xxx.com/xxx/json?data=";
$chh = curl_init();
curl_setopt($chh, CURLOPT_URL,$url_email);
curl_setopt($chh, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($chh, CURLOPT_POSTFIELDS, $str_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($str_data))
);
curl_setopt($chh, CURLOPT_RETURNTRANSFER, true);
curl_exec($chh);
curl_close($chh);
我没有得到o/p获取错误消息:
{"message":"ERROR","errorcode": "100" ,"errormessage":"Invalid JSON format used in API Call.Hence failed."}
怎么了
我想应该是:
$url_email="https://api.xxx.com/xxx/json"; // no `data` here
$chh = curl_init();
curl_setopt($chh, CURLOPT_URL, $url_email);
curl_setopt($chh, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($chh, CURLOPT_POSTFIELDS, 'data=' . $str_data); // `data` moved here