我是靠谱客的博主 儒雅飞机,这篇文章主要介绍php中使用fsockopen实现异步请求(代码示例),现在分享给大家,希望可以做个参考。

php执行一段程序,有可能几毫秒就执行完毕,也有可能耗时较长。

例如,用户下单这个事件,如果调用了些第三方服务进行发邮件、短信、推送等通知,可能导致前端一直在等待。

而有的时候,我们并不关心这些耗时脚本的返回结果,只要执行就行了。这时候就需要采用异步的方式执行。

众所周知,PHP没有直接支持多线程这种东西。我们可以采用折衷的方式实现。这里主要说的就是fsockopen

通过fsockopen发送请求并忽略返回结果,程序可以马上返回。

示例代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br />n"; } else { $out = "GET /backend.php HTTP/1.1rn"; $out .= "Host: www.example.comrn"; $out .= "Connection: Closernrn"; fwrite($fp, $out); /*忽略执行结果 while (!feof($fp)) { echo fgets($fp, 128); }*/ fclose($fp); }
登录后复制

需要注意的是我们需要手动拼出header头信息。通过打开注释部分,可以查看请求返回结果,但这时候又变成同步的了,因为程序会等待返回结果才结束。

实际测试的时候发现,不忽略执行结果,调试的时候每次都会成功发送sock请求;但忽略执行结果,经常看到没有成功发送sock请求。查看nginx日志,发现很多状态码为499的请求。

后来找到了原因:

fwrite之后马上执行fclose,nginx会直接返回499,不会把请求转发给php处理。

客户端主动端口请求连接时,NGINX 不会将该请求代理给上游服务(FastCGI PHP 进程),这个时候 access log 中会以 499 记录这个请求。

解决方案:

1)nginx.conf增加配置

复制代码
1
2
# 忽略客户端中断 fastcgi_ignore_client_abort on;
登录后复制

2)fwrite之后使用usleep函数休眠20毫秒:

复制代码
1
usleep(20000);
登录后复制

后来测试就没有发现失败的情况了。

附上完整代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php /** * 工具类 * */ class FsockService { public static function post($url, $param){ $host = parse_url($url, PHP_URL_HOST); $port = 80; $errno = ''; $errstr = ''; $timeout = 30; $data = http_build_query($param); // create connect $fp = fsockopen($host, $port, $errno, $errstr, $timeout); if(!$fp){ return false; } // send request $out = "POST ${url} HTTP/1.1rn"; $out .= "Host:${host}rn"; $out .= "Content-type:application/x-www-form-urlencodedrn"; $out .= "Content-length:".strlen($data)."rn"; $out .= "Connection:closernrn"; $out .= "${data}"; fwrite($fp, $out); //忽略执行结果;否则等待返回结果 // if(APP_DEBUG === true){ if(false){ $ret = ''; while (!feof($fp)) { $ret .= fgets($fp, 128); } } usleep(20000); //fwrite之后马上执行fclose,nginx会直接返回499 fclose($fp); } public static function get($url, $param){ $host = parse_url($url, PHP_URL_HOST); $port = 80; $errno = ''; $errstr = ''; $timeout = 30; $url = $url.'?'.http_build_query($param); // create connect $fp = fsockopen($host, $port, $errno, $errstr, $timeout); if(!$fp){ return false; } // send request $out = "GET ${url} HTTP/1.1rn"; $out .= "Host:${host}rn"; $out .= "Connection:closernrn"; fwrite($fp, $out); //忽略执行结果;否则等待返回结果 // if(APP_DEBUG === true){ if(false){ $ret = ''; while (!feof($fp)) { $ret .= fgets($fp, 128); } } usleep(20000); //fwrite之后马上执行fclose,nginx会直接返回499 fclose($fp); } } ?>
登录后复制

最后

以上就是儒雅飞机最近收集整理的关于php中使用fsockopen实现异步请求(代码示例)的全部内容,更多相关php中使用fsockopen实现异步请求(代码示例)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(111)

评论列表共有 0 条评论

立即
投稿
返回
顶部