zoukankan      html  css  js  c++  java
  • php异步处理

    <?php
    namespace IndexController;
    use CoreController;
     
    class test extends Controller
    {
        
        public function test11()
        {
            sleep(5);
            file_put_contents( './123.log', "123
    " , FILE_APPEND );
        }
     
        public function test12()
        {
            $url = 'http://127.0.0.1:1001/index/test/test11';
            $res = self::asyncRequest($url);
            echo "我没有等a站点返回值,我就执行了";
        }
     
        /**
         * php异步请求
         *
         * @param $host string 主机地址
         * @param $path string 路径
         * @param $param array 请求参数
         * @return string
         */
        public static function asyncRequest($url,$post_data=array(),$cookie=array())
        {
            $url_arr = parse_url($url);
            $port = isset($url_arr['port'])?$url_arr['port']:80;
     
            if($url_arr['scheme'] == 'https'){
                $url_arr['host'] = 'ssl://'.$url_arr['host'];
            }
     
            $fp = fsockopen($url_arr['host'],$port,$errno,$errstr,30);
            if(!$fp) return false;
     
            $getPath = isset($url_arr['path'])?$url_arr['path']:'/index.php';
     
            $getPath .= isset($url_arr['query'])?'?'.$url_arr['query']:'';
     
            $method = 'GET';  //默认get方式
     
            if(!empty($post_data)) $method = 'POST';
     
            $header = "$method  $getPath  HTTP/1.1
    ";
     
            $header .= "Host: ".$url_arr['host']."
    ";
     
            if(!empty($cookie)){  //传递cookie信息
                $_cookie = strval(NULL);
                foreach($cookie AS $k=>$v){
                    $_cookie .= $k."=".$v.";";
                }
                $cookie_str = "Cookie:".base64_encode($_cookie)."
    ";
                $header .= $cookie_str;
            }
     
            if(!empty($post_data)){  //传递post数据
                $_post = array();
                foreach($post_data AS $_k=>$_v){
                    $_post[] = $_k."=".urlencode($_v);
                }
     
                $_post = implode('&', $_post);
     
                $post_str = "Content-Type:application/x-www-form-urlencoded; charset=UTF-8
    ";
     
                $post_str .= "Content-Length: ".strlen($_post)."
    ";  //数据长度
     
                $post_str .= "Connection:Close
    
    ";
     
                $post_str .= $_post;  //传递post数据
     
                $header .= $post_str;
     
            }else{
                $header .= "Connection:Close
    
    ";
            }
     
            fwrite($fp, $header);
     
            usleep(1000); // 这一句也是关键,如果没有这延时,可能在nginx服务器上就无法执行成功
     
            fclose($fp);
     
            return true;
        }
     
    }

    转 :  https://www.cnblogs.com/xin-jun/p/10594625.html

  • 相关阅读:
    虚拟机安装VMware Tools
    SVN源码泄露漏洞
    为什么站点使用https加密之后还能看到相关数据
    AWVS11使用教程——Acunetix Web Vulnerability Scanner 11.x
    接口测试_RESTClient基本使用
    【pwnable.kr】coin1
    【pwnable.kr】 mistake
    【pwnable.kr】leg
    【pwnable.kr】random
    【pwnable.kr】passcode
  • 原文地址:https://www.cnblogs.com/fps2tao/p/11562171.html
Copyright © 2011-2022 走看看