How to make asynchronous HTTP requests in PHP

后端 未结 18 2044
梦如初夏
梦如初夏 2020-11-22 02:13

Is there a way in PHP to make asynchronous HTTP calls? I don\'t care about the response, I just want to do something like file_get_contents(), but not wait for

18条回答
  •  别跟我提以往
    2020-11-22 03:08

    Here is my own PHP function when I do POST to a specific URL of any page.... Sample: *** usage of my Function...

        
        $value)
            $values[]="$key=".urlencode($value);
            $data_string=implode("&",$values);
    
        // Find out which port is needed - if not given use standard (=80)
        if(!isset($URL_Info["port"]))
            $URL_Info["port"]=80;
    
        // building POST-request: HTTP_HEADERs
        $request.="POST ".$URL_Info["path"]." HTTP/1.1\n";
        $request.="Host: ".$URL_Info["host"]."\n";
        $request.="Referer: $referer\n";
        $request.="Content-type: application/x-www-form-urlencoded\n";
        $request.="Content-length: ".strlen($data_string)."\n";
        $request.="Connection: close\n";
        $request.="\n";
        $request.=$data_string."\n";
    
        $fp = fsockopen($URL_Info["host"],$URL_Info["port"]);
        fputs($fp, $request);
        while(!feof($fp)) {
            $result .= fgets($fp, 128);
        }
        fclose($fp); //$eco = nl2br();
    
    
        function getTextBetweenTags($string, $tagname) {
            $pattern = "/<$tagname ?.*>(.*)<\/$tagname>/";
            preg_match($pattern, $string, $matches);
            return $matches[1];
        }
        //STORE THE FETCHED CONTENTS to a VARIABLE, because its way better and fast...
        $str = $result;
        $txt = getTextBetweenTags($str, "span"); $eco = $txt;  $result = explode("&",$result);
        return $result[1];
        ".trim($_GET['em'])."
        
    "; }

提交回复
热议问题