[英]cURL post JSON array
Hi I'm having a problem with posting a JSON
array with cURL
to my API, 嗨,我在将带有
cURL
的JSON
数组发布到我的API时遇到问题,
I have this code below for the cURL
post: 我在
cURL
帖子下面有以下代码:
$data_string = stripslashes($JSONData);
$ch = curl_init('http://api.webadress.com');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
It doesn't store/post anything to the API end and the $results is not returning correct, What is wrong with the code? 它不存储/发布任何东西到API端,并且$ results返回的值不正确,代码有什么问题?
A bit from the JSON
: JSON
一点点:
{
"name": "test",
"type_id": "1",
"css": "#fb-iframe{}#fb-beforelike{}#fb-beforelike-blur{}",
"json": [
{
"Canvas": [
{
"Settings": {
"Page": {
"campaignName": "test"
}
},
"QuizModule": {
"Motivation": [],
"Questions": [],
"Submit_Fields": [
{
"label": "Name",
"name": "txtName",
"value": true
}
]
}
}
]
}
],
"user_id": "123"
}
Probably your $data_string
is not in field=value
pairs format and thus nothing is parsed in your $_POST
global. 您的
$data_string
可能不是field=value
对格式,因此在$_POST
全局field=value
没有任何内容。
Since you want to read from the $_POST
global: 由于您要从
$_POST
全局读取:
content-type
content-type
$data_string
must be in field=value
pairs format $data_string
必须采用“ field=value
对格式 The following will work (I have omitted altogether the header part, you should not set the content-type
): 以下将起作用(我完全省略了标题部分,不应设置
content-type
):
$data_string = stripslashes($JSONData);
$ch = curl_init('http://api.webadress.com');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, array('JSONData'=>$data_string));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
If on the other hand you want to access the data as you already send them, then you shouldn't try to read them through $_POST
but instead use on the server side: 另一方面,如果您想访问已发送的数据,则不应尝试通过
$_POST
读取它们,而应在服务器端使用:
$JSONData = file_get_contents("php://input");
您需要对其进行http_build_query()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.