How does post submit raw data in PHP?

Source: Internet
Author: User

is debug using the POSTMAN tool, the raw data submitted in the form will return to the bottom of the string of data;

Instead, the form-urlencoded data that was submitted in the form of the file is returned with an error. The same is true with the 1th form-data form.

Now the situation is that I'm PHP using curl the post same error as the data that is shown.

The code is as follows:

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;

?>

Evil Split Line: Resolved

Resolved:
This has nothing to do with the Post method. It's because there's a missing 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"     );

The final pattern of the code is:

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;

POSTData is still in the json format of the data, not array the form. POSTMANIt's been given to a hole ~!!!

Reply content:

is debug using the POSTMAN tool, the raw data submitted in the form will return to the bottom of the string of data;

Instead, the form-urlencoded data that was submitted in the form of the file is returned with an error. The same is true with the 1th form-data form.

Now the situation is that I'm PHP using curl the post same error as the data that is shown.

The code is as follows:

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;

?>

Evil Split Line: Resolved

Resolved:
This has nothing to do with the Post method. It's because there's a missing 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"     );

The final pattern of the code is:

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;

POSTData is still in the json format of the data, not array the form. POSTMANIt's been given to a hole ~!!!

http://stackoverflow.com/questions/13099177/how-to-send-raw-post-data-with-curl-php
It is said to set an unrecognized Content-Type , postfileds or pass an array as before.

Raw is the most primitive data in the HTTP request entity content body, you can customize the body data sent to the server, and set the header to determine the type of body for the server to resolve. If you do not set Content-type, the default is x-www-form-urlencoded. Body in the form of parameter =& parameter =

curl_setopt($handle, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));

has been resolved, thank you.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.