Home >php教程 >php手册 >PHP异步调用socket简单实例

PHP异步调用socket简单实例

WBOY
WBOYOriginal
2016-05-25 16:39:151326browse

文章利用了socket来实现异步调用,有需要学习socket的朋友可以参考一下本文章以及参考资料,代码如下:

<?php 
    $host = "www.phprm.com"; 
    $path = "/Report.php?ReportID=1"; 
    $cookie = Session_id(); 
    
    $fp = fsockopen($host, 80, $errno, $errstr, 30); 
    if (!$fp) { 
       print "$errstr ($errno)<br />n"; 
       exit; 
    } 
    $out = "GET ".$path." HTTP/1.1rn"; 
    $out .= "Host: ".$host."rn"; 
    $out .= "Connection: Closern"; 
    $out .= "Cookie: ".$cookie."rnrn"; 
    fwrite($fp, $out);  //将请求写入socket 
    //也可以选择获取server端的响应 
    /*while (!feof($fp)) { 
        echo fgets($fp, 128); 
    }*/ 
    //如果不等待server端响应直接关闭socket即可 
    fclose($fp);


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn