是使用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'));
已解決,謝謝。