PHP input/output stream

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 using HTTP requests:

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 Bodyreq.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 responds to the data printed out!

The data.php code is

Print_r ($_post);

Print the data passed!

The result of running at the command line is

You can see that the array is empty, that 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 of raw data that can access the request. POST request, it is best to use php://input instead of the $HTTP _raw_post_data because it does not depend on a specific php.ini directive. And, in such a case $HTTP _raw_post_data is not populated by default, with less memory than the activation Always_populate_raw_post_data potentially requires. Enctype= "Multipart/form-data" when the Php://input is invalid.

$_post is only available when the data is submitted by application/x-www-form-urlencoded type, the Enctype property of the form 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 action is get, the browser uses x-www-form-urlencoded encoding to convert the form data into a string (Name1=value1&name2=value2 ... ), and then append the string to the URL, using the. Split, to load the new URL. When the action is post, the browser encapsulates the form data into the HTTP body and then sends it to the server.

When we change the delivery options to

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

Plus a headers content-type can be used to receive data $_post! If this is not the form type, you can receive the data with the original input!

  • 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.