PHP Fsocketopen with socket analog post

Source: Internet
Author: User

When I heard about this functional requirement, I was immediately blindfolded and didn't lie to everyone! First of all, PHP socket has not been used, and then did not hear the post can also use PHP socket to simulate it! Later found a case to see, just understand that is not so advanced, but the battlefield is not clear the principle of post and the essence of it, in fact, the essence is sent to the purpose program a sign for the post of the Protocol string as follows:


POST/Purpose Program http/1.1
Accept: */*
accept-language:zh-cn,en-us;q=0.5
content-type:application/x-www-form-urlencoded
user-agent:mozilla/4.0 (compatible; MSIE 5.0; Windows NT;. NET CLR 1.0.3705;. NET CLR 1.1.4322)
Host: Hosting address to send to
Content-length: The length of the data sent
Pragma:no-cache
Cache-control:no-cache
Data sent by Username=php&password=iask//post

I think you should be most familiar with the Post method of form, for example, when we want to send a username and password to a page, click the Submit button of a form with two text fields, this form is sent to the action program is the above data. I don't think it's difficult to know that.

At this time we only need to use PHP socket open a port, such as 80 ports, the above information to use this port to send to the destination program on the line.

How do we create a socket channel on a single port?
It's so simple in PHP!

Fsocketopen (host name, port number, accept variable for error number, accept variable for error prompt, timeout time)

The host name is the destination where you need to send the data;
The port number is the destination where the program will wait for your data;
The acceptance variable for the error number, which is the error number that is returned if the socket is not successfully established;
Error-prompted variable, is the wrong time to return the error message;
Timeout is the maximum time to wait after post data if the other person has not responded to the message.

If there is no accident (you correctly set the parameters of the Fsocketopen () function), a socket channel is now open, and what we need to do next is to send the POST request protocol to the destination program through this open channel. At this point, you can use either of the fwrite or fputs functions to send the Post's request format to the open handle of the Fsocketopen (), at which point a great socket emulation Post request is born. By the way:

You can not send my POST request format directly Ctrl + V in the past, you do not see the Chinese characters waiting for your own to fill it out?

The above is the description of the text, if you think that said very rubbish, very unconvincing words, there is a ready-made program fragment, we can see.


$port = $url [' Port ']? $url [' Port ']: 80;
$fp = Fsockopen ($url [' Host '], $port, $errno, $ERRSTR, 10);
if (! $fp) return "failed to open the socket on host: $url [host] $port failed because: $errno-$errstr";
Fputs ($FP, sprintf ("POST%s%s%s http/1.0rn", $url [' Path '], $url [' query ']? "?": "", $url [' query ']]);
Fputs ($FP, "host:". $url [' Host ']. " RN ");
Fputs ($fp, "content-type:application/x-www-form-urlencodedrn");
Fputs ($FP, "Content-length:". Strlen ($encoded). "RN");
Fputs ($fp, "connection:closernrn");
Fputs ($fp, "$encoded");

By the way, fgets ($FP, 1024); This can get the return value of Oh!! More than one person has asked me how to accept the return!!

Related Article

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.