上图是使用POSTMAN
工具调试的,用raw
形式提交数据就会返回最底部的那串数据;
而下图,则是使用form-urlencoded
形式提交的数据,返回错误了。使用第1种form-data
形式也是如此。
现在的情况是,我使用PHP
的curl
来post
数据,则如上图所示一样的错误。
代码如下:
function curls($url, $data_string) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-AjaxPro-Method:ShowList',
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($data_string))
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$get_url = "http://abc.xxx.com/UC_News_ShowList,App_Web_abc.ashx";
$post_str = '{"P":5,"Sclass":"新闻","Table":"HS_N_News","Link":"News"}';
$post_datas = curls($get_url, $post_str);
echo $post_datas;
?>
已解决:
这个和post方式无关。而是因为少了个header
$headers = array(
"User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36",
"X-AjaxPro-Method:ShowList"
);
代码最终形态是:
function curls($url, $data_string) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-AjaxPro-Method:ShowList',
'User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36'
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$get_url = "http://abc.xxx.com/UC_News_ShowList,App_Web_abc.ashx";
$post_str = '{"P":5,"Sclass":"新闻","Table":"HS_N_News","Link":"News"}';
$post_datas = curls($get_url, $post_str);
echo $post_datas;
POST
的数据依然是json
格式的数据,而不是array
形式的。被POSTMAN
给坑了~!!!
上图是使用POSTMAN
工具调试的,用raw
形式提交数据就会返回最底部的那串数据;
而下图,则是使用form-urlencoded
形式提交的数据,返回错误了。使用第1种form-data
形式也是如此。
现在的情况是,我使用PHP
的curl
来post
数据,则如上图所示一样的错误。
代码如下:
function curls($url, $data_string) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-AjaxPro-Method:ShowList',
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($data_string))
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$get_url = "http://abc.xxx.com/UC_News_ShowList,App_Web_abc.ashx";
$post_str = '{"P":5,"Sclass":"新闻","Table":"HS_N_News","Link":"News"}';
$post_datas = curls($get_url, $post_str);
echo $post_datas;
?>
已解决:
这个和post方式无关。而是因为少了个header
$headers = array(
"User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36",
"X-AjaxPro-Method:ShowList"
);
代码最终形态是:
function curls($url, $data_string) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-AjaxPro-Method:ShowList',
'User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36'
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$get_url = "http://abc.xxx.com/UC_News_ShowList,App_Web_abc.ashx";
$post_str = '{"P":5,"Sclass":"新闻","Table":"HS_N_News","Link":"News"}';
$post_datas = curls($get_url, $post_str);
echo $post_datas;
POST
的数据依然是json
格式的数据,而不是array
形式的。被POSTMAN
给坑了~!!!
http://stackoverflow.com/questions/13099177/how-to-send-raw-post-data-with-curl-php
据说设置一个不可识别的Content-Type
,POSTFILEDS还是像以前一样传递一个Array就可以了
raw 就是http请求实体内容body中最原始的数据,可以自定义发送给服务器的body数据,并通过设置header来确定body的类型来供服务器解析。如果不设置content-type的话,默认为x-www-form-urlencoded。body中为参数=&参数=的形式
curl_setopt($handle, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
已解决,谢谢。