PHP异步操作方法

<?php
function http_request( 
    $verb = 'GET',             /* HTTP Request Method (GET and POST supported) */
     $ip,                       /* Target IP/Hostname */ 
    $port = 80,                /* Target TCP port */ 
    $uri = '/',                /* Target URI */ 
    $getdata = array(),        /* HTTP GET Data ie. array('var1' => 'val1', 'var2' => 'val2') */
    $postdata = array(),       /* HTTP POST Data ie. array('var1' => 'val1', 'var2' => 'val2') */
    $cookie = array(),         /* HTTP Cookie Data ie. array('var1' => 'val1', 'var2' => 'val2') */
    $custom_headers = array(), /* Custom HTTP headers ie. array('Referer: http://localhost/ */
    $timeout=array('asyn'=>true,1000),           /* Socket timeout in milliseconds */ 
    $req_hdr = false,          /* Include HTTP request headers */ 
    $res_hdr = false           /* Include HTTP response headers */ 
    ) 
{ 
    $ret = ''; 
    $verb = strtoupper($verb); 
    $cookie_str = ''; 
    $getdata_str = count($getdata) ? '?' : ''; 
    $postdata_str = ''; 


    foreach ($getdata as $k => $v) 
                $getdata_str .= urlencode($k) .'='. urlencode($v) . '&'; 


    foreach ($postdata as $k => $v) 
        $postdata_str .= urlencode($k) .'='. urlencode($v) .'&'; 


    foreach ($cookie as $k => $v) 
        $cookie_str .= urlencode($k) .'='. urlencode($v) .'; '; 


    $crlf = "\r\n"; 
    $req = $verb .' '. $uri . $getdata_str .' HTTP/1.1' . $crlf; 
    $req .= 'Host: '. $ip . $crlf; 
    $req .= 'User-Agent: Mozilla/5.0 Firefox/3.6.12' . $crlf; 
    $req .= 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' . $crlf;
     $req .= 'Accept-Language: en-us,en;q=0.5' . $crlf; 
    $req .= 'Accept-Encoding: deflate' . $crlf; 
    $req .= 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7' . $crlf; 
    
    foreach ($custom_headers as $k => $v) 
        $req .= $k .': '. $v . $crlf; 
        
    if (!empty($cookie_str)) 
        $req .= 'Cookie: '. substr($cookie_str, 0, -2) . $crlf; 
        
    if ($verb == 'POST' && !empty($postdata_str)) 
    { 
        $postdata_str = substr($postdata_str, 0, -1); 
        $req .= 'Content-Type: application/x-www-form-urlencoded' . $crlf; 
        $req .= 'Content-Length: '. strlen($postdata_str) . $crlf . $crlf; 
        $req .= $postdata_str; 
    } 
    else $req .= $crlf; 
    
    if ($req_hdr) 
        $ret .= $req; 
    
    if (($fp = @fsockopen($ip, $port, $errno, $errstr)) == false) 
        return "Error $errno: $errstr\n"; 
    if($timeout['asyn']){
        stream_set_timeout($fp, 0, $timeout['time']);//这里是异步关键,没有这个,测试发现不能异步。
    }
    
    fputs($fp, $req); 
    while ($line = fgets($fp)) $ret .= $line; 
    fclose($fp); 
    if (!$res_hdr) 
        $ret = substr($ret, strpos($ret, "\r\n\r\n") + 4); 
    return $ret; 
}




$baseUrl = str_replace('\\','/',dirname($_SERVER['SCRIPT_NAME']));
$baseUrl = empty($baseUrl) ? '/' : '/'.trim($baseUrl,'/').'/';
echo $baseUrl.'backgroud.php';


$getDataArr=array();
$postDataArr=array('name'=>'zhangshan','age'=>19);
$cookieDataArr=array();
$customHeader=array();
$timeout=array('asyn'=>true,'time'=>3000);//毫秒  //是否异步
$req_hdr = false;          /* Include HTTP request headers */ 
$res_hdr = true;          /* Include HTTP response headers */
$url = $baseUrl.'background.php';
$str = http_request('POST','127.0.0.1',80, $url,$getDataArr,$postDataArr,$cookieDataArr,$customHeader,$timeout,$req_hdr,$res_hdr); //异步
file_put_contents("action.txt", $str);
echo '结束';
</pre>
background.php 内容测试  
<pre name="code" class="php"><?php
set_time_limit(0);
for($i=0;$i<5;$i++){
    file_put_contents("background.txt", print_r($_POST, true));
    sleep(10);
}
echo date('Y-m-d H:i:s').'end!';
?>
以上我第一份PHP工作中写的,老板当时说PHP后端发送几百上千份email,而且要立即返回已接受处理状态给前端。
通过以上PHP代码可完成异步发送大量eamil或其他费时操作。
 
 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值