PHP Fsockopen Analog Post/get method

Source: Internet
Author: User
Tags fread unix domain socket

Fsockopen is a very powerful function, support socket programming, you can use Fsockopen to implement the mail and other socket programs, etc., using fcockopen need to manually stitching out the header section. For details, refer to the official documentation: http://cn2.php.net/fsockopen/.

Resource Fsockopen string $hostname $port = 1 [, int &$errnostring &$errstrfloat$timeout Ini_get ("Default_socket_timeout")] []] )

Fsockopen-open Internet or Unix domain socket connection opens the socket link for the network.

Currently, this function provides two Socket file streams using the Af_inet and Unix Af_unix for the Internet.

    • When used in the Internet situation, the parameters hostname and port respectively represent the URL and port number.
    • In the UNIX scenario, the Ipc,hostname parameter is represented to the socket path, and port is configured to 0.

The timeout option that can be omitted indicates how long it has been interrupted without a connection. After using this function, a file pointer is returned for use by the file function, including fgets (), FGETSS (), fputs (), fclose (), and feof (). Parameters errno and Errstr can also be omitted, mainly as error handling use. With this function, the shelve mode (blocking mode) is used, and the set_socket_blocking () can be converted to no-hold mode.

The following uses Fsockopen to simulate the generation of HTTP connections.

Fsockopen, in addition to the previous section of the simulation to generate an HTTP connection, can also implement a lot of functions, such as analog post and get Transfer data method. For example, the following:

$fp=Fsockopen("Www.cnblogs.com", 80,$errno,$errstr, 30);if(!$fp){    Echo"$errstr($errno) <br/>\n ";}Else{    $out= "get/http/1.1\r\n"; $out. = "host:www.cnblogs.com\r\n"; $out. = "connection:close\r\n\r\n"; fwrite($fp,$out); $content= ' ';  while(!feof($fp))    {        $content.=fgets($fp, 128); }    Echo $content; fclose($fp);}

After executing this code, the return information is displayed, as well as the HTML code for this page:

http/1.1 OKDate: Sun, 01:41:56 gmtcontent-type:text/html; Charset=utf-8Content-length:44177Connection: closevary: accept-encodingcache  Public Max-age=104Expires: Sun, 01:43:33 gmtlast-modified:sun, 01:41:33
   
     gmtx-aspnetmvc-version:3.0
    x-aspnet-version:4.0.30319
    x-powered-by:asp. 
    NETX-ua-compatible:ie=10
   

ps:php Fsockopen requires php.ini in the Allow_url_fopen option to open.

//Fsocket Analog Post submission$url= "Http://localhost/test2.php?site=nowamagic.net";Print_r(Parse_url($url));//echo $query;Sock_get ($url, "User=gonn");//Sock_get ($url, $query);//fsocket Analog Get CommitfunctionSock_get ($url,$query){    $data=Array(    ' foo ' = ' bar ', ' baz ' = ' boom ', ' site ' = ' www.nowamagic.net ', ' name ' = ' Nowa Magic '); $query _str=Http_build_query($data); $info=Parse_url($url); $fp=Fsockopen($info["Host"], 80,$errno,$errstr, 3); //$head = "GET". $info [' Path ']. "?". $info ["Query"]. " Http/1.0\r\n ";    $head= "GET".$info[' Path ']. "?".$query _str." Http/1.0\r\n "; $head. = "Host:".$info[' Host ']. \ r \ n "; $head. = "\ r \ n"; $write=fputs($fp,$head);  while(!feof($fp))    {        $line=fread($fp, 4096); Echo $line; }}sock_post ($url, "User=gonn");functionSock_post ($url,$query){        $info=Parse_url($url); $fp=Fsockopen($info["Host"], 80,$errno,$errstr, 3); $head= "POST".$info[' Path ']. "?".$info["Query"]. " Http/1.0\r\n "; $head. = "Host:".$info[' Host ']. \ r \ n "; $head. = "referer:http://".$info[' Host '].$info[' Path ']. \ r \ n "; $head. = "content-type:application/x-www-form-urlencoded\r\n"; $head. = "Content-length:".strlen(Trim($query))." \ r \ n "; $head. = "\ r \ n"; $head.=Trim($query); $write=fputs($fp,$head);  while(!feof($fp))    {        $line=fread($fp, 4096); Echo $line; }}

The code to receive the page test2.php is:

$data $_request ; Echo ' <pre> '; Print_r $data  ); Echo ' </pre> ';

PHP Fsockopen Analog Post/get method

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.