ITPub博客

首页 > IT职业 > IT图书 > php中用来可以做异步调用的代码

php中用来可以做异步调用的代码

IT图书 作者:choubou 时间:2021-09-09 09:36:06 0 删除 编辑


        static public function getAsn($url,$errno='',$errstr='',$time_out = 5) {        //移除url中的空格,如果可以格式化url,或许会更好        $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.1rn";        $out .= "Host: ".$arr['host']."rn";        $out .= "Connection: Closernrn";        fwrite($fp,$out);        fclose($fp);    }        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){            //在这里还可以使用 http_build_query() 函数,将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.1rn";        $out .= "Host: ".$arr['host']."rn";        $out .= "Content-type:application/x-www-form-urlencodedrn";          $out .= "Connection: Closern";        $out .= "Content-Length:$data_lenrnrn";         $out .= $post_data."rn";        fwrite($fp,$out);        fclose($fp);    }


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4548/viewspace-2810890/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-10-08

  • 博文量
    175
  • 访问量
    10712