PHP中如何POST提交raw資料?

來源:互聯網
上載者:User

是使用POSTMAN工具調試的,用raw形式提交資料就會返回最底部的那串資料;

而,則是使用form-urlencoded形式提交的資料,返回錯誤了。使用第1種form-data形式也是如此。

現在的情況是,我使用PHPcurlpost資料,則如所示一樣的錯誤。

代碼如下:

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形式也是如此。

現在的情況是,我使用PHPcurlpost資料,則如所示一樣的錯誤。

代碼如下:

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'));

已解決,謝謝。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.