上图是使用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'));
已解决,谢谢。