Sample code of php input/output stream (Figure)

Source: Internet
Author: User
This article mainly introduces the php input and output stream. relevant information and simple sample code are organized here. if you need it, refer to the following section to introduce the php input and output stream, I have sorted out the relevant information and simple sample code. if you need it, you can refer

I have recently learned the http protocol! To better understand the http protocol, I checked the http module of nodejs! I think there are still a lot of gains. For example, I use an http request to send a 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 bodyreq.end('name=liuzhang&age=28');

The above code indicates sending data 'name = liuzhang & age = 28'. the callback is the response object and prints the server response data!

Data. php code is

print_r($_POST);

Print the passed data!

The result of running on the command line is

Received data!

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 rely on specific php. ini commands. In this case, $ HTTP_RAW_POST_DATA is not filled by default, which requires less memory than activating always_populate_raw_post_data. When enctype = "multipart/form-data", php: // input is invalid.

$ _ POST can only be obtained when data is submitted by application/x-www-form-urlencoded type. the enctype attribute of form is encoded in two ways: application/x-www-form-urlencoded and multipart/form-data. the default value is application/x-www-form-urlencoded. When the action is get, the browser uses the x-www-form-urlencoded encoding method to convert form data into a string (name1 = value1 & name2 = value2 ...), then append the string to the end of the url, using? Load the new url. When the action is post, the browser encapsulates form data into the http body and sends it to the server.

When we change "send options" to "send options ",

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

With a headers content-type, you can use $ _ POST to receive data! If this is not the form type, you can use the original input to receive data!

The above is a detailed explanation of the sample code of the php input/output stream (figure). For more information, see other related articles in the first PHP community!

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.