PHP analog send POST request three, with Telnet and fsockopen () analog send post information, telnetfsockopen_php tutorial

Source: Internet
Author: User

PHP simulates sending post requests using Telnet and fsockopen () to send post messages, Telnetfsockopen

Knowing the specifics of the HTTP header and URL information, we started to try to write a header message to the server. Windows built-in command telnet helps us to send a simple HTTP request.

And Telnet is a particularly flexible tool, and we can use it for simple requests such as FTP/SMTP/POP3/TCP.

From the Start menu-run the--cmd command to enter the DOS state,

We enter "Telnet host address port (telnet 80)" To enter the Telnet command state (completely black window, when the output character will be problematic), we press "CTRL" + "]", cut back to the normal CMD window, and then press "ENTER" Enter the edit command state.

If you are prompted not to find the Telnet command, you can unlock the Telnet command as follows.

After entering the Telnet command input mode, we can directly edit the header file to send

Just note: When you finish editing the request header file, press the ENTER key two times to send.

This is a standard HTTP request and response, and is not associated with the HTTP file of the previous section.

Of course, we can not only use such inconvenient tools, the following is to introduce PHP Fsockopen () method.

First look at the prototype of the Fsockopen () method:

Resource Fsockopen (string $hostname [, int $port =-1 [, int & $errno [, String & $errstr [, float $timeout = ini_ Get ("Default_socket_timeout")]])

It returns the result of a resource type, and the parameters that need to be passed in are:

$hostname: Host Name

$port: Port number

$errno: Error Number (note the "&" in the prototype will be assigned to $errno once the connection has an error)

$ERRSTR: Error string (Error message will be assigned to $ERRSTR once connection is incorrect)

$timeout: Time Out

$host=parse_url($url, php_url_host); // Get Host Data $port=parse_url($url, php_url_port) = =nullparse_url($url , Php_url_port); // get port data, if not set then default to $path=parse_url($url, Php_url_path); // gets the path information to use after $socket=fsockopen($host,$port,$errno,$errstr , 20);//Get resource type $socket

This resource type is the same as in Telnet where we use the Telnet host name Port number command after the environment.

So next, we're going to write about this environment.

We can use the Fwrite () method to write header information over and over again, or to flatten the header information once.

fwrite ($socket, "POST".) $path. " http/1.1\r\n ");     fwrite ($socket, "host:localhost\r\n\r\n");

or flatten and re-write the first way:

$str= "POST". $path. " Http/1.1\r\n "." host:localhost\r\n\r\n "; fwrite ($socket,$str);

Note: There is a return key between the request line and the header, we use "\ r \ n" to enter the input, the end of the dual-use "\r\n\r\n" simulation two times enter.

Next we use Fread to read the response information and save it in $info:

 while (! feof ($socket)) {          // when the content remains to be read    $infofgets($socket, 4096);  // reads 4,096 bytes of content at a time and connects connect prompt to $info. echo $info ;  // The output returns the result. 

Of course, the above is a particularly simple example, we can make it more perfect by flattening more header information (join accept, etc.).

Next we'll talk about some common post methods.

If you think this article is helpful to you, please do a bit of recommendation, if there is any problem, you can comment on the bottom of the joint discussion, thank you. true techarticle PHP analog send POST request three, with Telnet and fsockopen () analog send post information, Telnetfsockopen understand the HTTP header information and URL information details, we began to try to do it ourselves ...

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