PHP input and output flow details and example code _php instance

Source: Internet
Author: User

Recently learning the HTTP protocol! To better understand the HTTP protocol, look at the Nodejs HTTP module! Feel the harvest is still quite a lot. For example, I send a request with the HTTP request:

var options = {
 host: ' localhost ',
 port:80,
 path: '/backbone/data.php ', method
 : ' POST '
};


var req = http.request (options, function (res) {
 console.log (' STATUS: ' + res.statuscode);
 Console.log (' HEADERS: ' + json.stringify (res.headers));
 Res.setencoding (' UTF8 ');
 Res.on (' Data ', function (chunk) {
  console.log (' body: ' + chunk ');
 }
); Write data to request the body
req.end (' name=liuzhang&age=28 ');

The above code means to send the data ' name=liuzhang&age=28 ', the callback is the object of the response, the server response data print out!

data.php Code is

Print_r ($_post);

Print the data sent over!

The result of running at the command line is

Can see array is empty, is $_post no data, at first I thought the data did not pass over! But I changed the back-end data.php

Echo file_get_contents ("Php://input");

Received the data sent over!

Php://input is a read-only stream that can access the requested raw data. In the case of POST requests, it is best to use php://input instead of $HTTP _raw_post_data because it does not depend on specific php.ini directives. Also, in such cases $HTTP _raw_post_data is not populated by default, and less memory is potentially needed than activating always_populate_raw_post_data. Enctype= "Multipart/form-data" when the Php://input is invalid.

$_post only when the data is submitted by application/x-www-form-urlencoded type, the form's Enctype property is encoded and is commonly used in two ways: application/ X-www-form-urlencoded and Multipart/form-data, default to application/x-www-form-urlencoded. When the action is get, the browser converts the form data into a string (Name1=value1&name2=value2 ...) using x-www-form-urlencoded encoding. , and then append the string to the URL and use the? Split to load the new URL. When the action is post, the browser encapsulates the form data into the HTTP body and sends it to the server.

When we change the sending options to

var options = {
 host: ' localhost ',
 port:80,
 path: '/backbone/data.php ', method
 : ' POST ',
 headers: {' Content-type ': ' application/x-www-form-urlencoded '}
};

Add a headers Content-type can receive data with $_post! If this is not the form type, you can receive the data with the original input!

The above is the PHP input and output stream do the data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!

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.