温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-07-25 17:09:23 阅读:314 作者:xiao5461 栏目:web开发
PHP开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>
    /**
     * 发送一个异步http协议的Get请求,不用关心结果
     * @param  $url
     * @param  $errno
     * @param  $errstr
     * @param  $time_out
     */
    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.1\r\n";
        $out .= "Host: ".$arr['host']."\r\n";
        $out .= "Connection: Close\r\n\r\n";
        fwrite($fp,$out);
        fclose($fp);
    }
    /**
     * 异步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){
            //在这里还可以使用 http_build_query() 函数,将post的内容编码
            foreach ($post_arr as $key => $val){
                $post_data .= urlencode($key) ."="urlencode($val)."&";
            }
            $post_data substr($post_data0,-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);
    }

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×