本篇文章为大家展示了如何异步请求处理函数,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
sendmail.php代码如下:
<?php
$name = $_POST['name'];
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "Bill $name\n";
fwrite($myfile, $txt);
$txt = "Steve Jobs\n";
fwrite($myfile, $txt);
fclose($myfile);
index.php代码如下:
<?php
header("Content-type: text/html; charset=utf8");
function doRequest($url, $param=array()){
$urlinfo = parse_url($url);
$host = $urlinfo['host'];
$path = $urlinfo['path'];
$query = isset($param)? http_build_query($param) : '';
$port = 80;
$errno = 0;
$errstr = '';
$timeout = 10;
$fp = fsockopen($host, $port, $errno, $errstr, $timeout);
$out = "POST ".$path." HTTP/1.1\r\n";
$out .= "host:".$host."\r\n";
$out .= "content-length:".strlen($query)."\r\n";
$out .= "content-type:application/x-www-form-urlencoded\r\n";
$out .= "connection:close\r\n\r\n";
$out .= $query;
fputs($fp, $out);
fclose($fp);
}
echo time().'<br>';
$url = 'http://demo.study.com/sendmail.php';
$param = array(
'name'=>'ceshi'
);
doRequest($url, $param);
echo time().'<br>';
上述内容就是如何异步请求处理函数,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注编程网行业资讯频道。