Code used in PHP to make asynchronous calls

Source: Internet
Author: User
Tags urlencode

Tag:php    Async     code    

    /**     *  send a GET request for an asynchronous HTTP protocol without caring about the results       *  @param    $url      *  @param    $errno       *  @param    $errstr      *  @param     $time _out     */    static public function  GETASN ($url, $errno = ", $errstr =", $time _out = 5)  {         //remove spaces from the URL, if you can format the URL, it might be better          $url  = str_replace ('   ',  ',  $url);             $arr  = parse_url ($url);         $arr [' Port '] | |   $arr [' Port '] = 80;         $fp  = fsockopen ($ arr[' host ', $arr [' Port '], $errno, $ERRSTR, $tIme_out);         if (! $fp)  {             return  $errno. "   ". $errstr;        }                  $arr [' query '] &&  $arr [' Query '] =   '? '. $arr [' query '];         $out  =  ' get  '. $arr [' path ']. $arr [' Query '].  http/1.1\r\n ";         $out  .= " host:  ". $arr [' Host ‘]." \ r \ n ";         $out  .= " connection: close\r\n\r\n ";         fwrite ($fp, $out);         fclose ($FP);    }    /**     *  Asynchronous post     *  @param    $url      *  @param    $post _arr     * @ param   $errno      *  @param    $errstr       *  @param    $time _out     */    static      FUNCTION POSTASN ($url, $post _arr, $errno  =  ", $errstr =", $time _out =  5)  {         $arr  = parse_url ($url);          $arr [' Port '] | |   $arr [' Port '] = 80;          $fp  =  Fsockopen ($arr [' Host '], $arr [' Port '], $errno, $errstr, $time _out);         if (! $fp)  {            return  $errno. "   ". $errstr;         }         $post _data =  "";         if ($post _arr) {            //can also be used here  http_build_query ()   function to encode the contents of the Post             foreach  ($post _arr as  $key  =>  $val) {                  $post _data .= urlencode ($key)  . " = ".  urlencode ($val)." & ";            }              $post _data = substr ($post _data, 0,-1);         }         $data _len =  strlen ($post _data);           $arr [' Query '] &&   $arr [' QuerY '] =  '?. $arr [' query '];         $out  =  ' post  '. $arr [' path ']. $arr [ ' Query ']. "  http/1.1\r\n ";         $out  .= " host:  ". $arr [' Host ‘]." \ r \ n ";         $out  .= " content-type:application/ x-www-form-urlencoded\r\n ";           $out  .= " connection: close\r\n ";         $out  .= " Content-Length: $data _len\r\n\r\n ";          $out  .=  $post _data." \ r \ n ";         fwrite ($fp, $out);         fclose ($fp);     }


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.