Fsockopen Get,post Package

Source: Internet
Author: User

functionHttp_request ($url,$method= ' GET ',$postfields=NULL,$headers=Array()) {     $parse=Parse_url($url); isset($parse[' Host ']) ||$parse[' host '] = '; isset($parse[' Path ']) ||$parse[' path '] = '; isset($parse[' Query ']) ||$parse[' query '] = '; isset($parse[' Port ']) ||$parse[' port '] = '; $path=$parse[' Path ']?$parse[' Path ']. ($parse[' Query ']? ‘?‘.$parse[' Query ']: '): '/'; $host=$parse[' Host ']; //Protocol    if($parse[' scheme '] = = ' HTTPS ') {        $version= ' 1.1 '; $port=Empty($parse[' Port ']) ? 443:$parse[' Port ']; $host= ' ssl://'.$host; } Else {        $version= ' 1.0 '; $port=Empty($parse[' Port ']) ? 80:$parse[' Port ']; }     //Headers    $headers[] = "Host: {$parse[' Host ']} "; $headers[] = ' connection:close '; $headers[] = "User-agent:$_server[Http_user_agent] "; $headers[] = ' Accept: */* '; //Package Body Information    if($method= = ' POST ') {        if(Is_array($postfields)){            $postfields=Http_build_query($postfields); }        $headers[] = "content-type:application/x-www-form-urlencoded"; $headers[] = ' content-length: '.strlen($postfields); $out= "POST$pathhttp/$version\ r \ n ".Join("\ r \ n",$headers)." \r\n\r\n ".$postfields; } Else {        $out= "GET$pathhttp/$version\ r \ n ".Join("\ r \ n",$headers)." \r\n\r\n "; }     //Send Request    $limit= 0; $fp=Fsockopen($host,$port,$errno,$errstr, 30); if(!$fp) {        Exit(' Failed to establish socket connection: '.$url); } Else {        $header=$content= ' '; //Set block/nonblocking mode stream, $block ==true apply flow mode        stream_set_blocking($fp,true); //to set the time-out for a stream        Stream_set_timeout($fp, 30); fwrite($fp,$out); //Get Header/metadata from the encapsulated protocol file pointer        $status=Stream_get_meta_data($fp); if(!$status[' Timed_out ']) {//not timed out             while(!feof($fp)) {                $header.=$h=fgets($fp); if($h&& ($h= = "\ r \ n" | |$h= = "\ n")) Break; if(Strpos($h, ' content-length: ')!==false) {                    $limit=intval(substr($header, 15)); }            }             $stop=false;  while(!feof($fp) &&!$stop) {                $data=fread($fp, ($limit= = 0 | |$limit> 8192? 8192:$limit)); $content.=$data; if($limit) {                    $limit-=strlen($data); $stop=$limit<= 0; }            }        }        fclose($fp); //Unchunk        $content=Preg_replace_callback(            ‘/(?:(?:\ r\n|\n) |^) ([0-9a-f]+) (?: \            r\n|\n) {. *?) '. ‘((?:\ r\n|\n) (?: [0-9a-f]+ (?: \ r\n|\n) |$)/si ',create_function(                ' $matches ', ' Return Hexdec ($matches [1]) = = strlen ($matches [2])? $matches [2]: $matches [0]; '            ),$content        ); return $content; }}

Execute the script asynchronously by adding the following code:

Ignore_user_abort (TRUE);//If the client disconnects, it will not cause script abortset_time_limit (0);//Cancel script execution delay limit

Fsockopen Get,post Package (RPM)

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.