index.php

    /**
* php异步请求
*
* @param $host string 主机地址
* @param $path string 路径
* @param $param array 请求参数
* @return string
*/
function asyncRequest($url,$post_data=array(),$cookie=array())
{
$url_arr = parse_url($url);
$port = isset($url_arr['port'])?$url_arr['port']:80; if($url_arr['scheme'] == 'https'){
$url_arr['host'] = 'ssl://'.$url_arr['host'];
}
$fp = fsockopen($url_arr['host'],$port,$errno,$errstr,30);
if(!$fp) return false;
$getPath = isset($url_arr['path'])?$url_arr['path']:'/index.php';
$getPath .= isset($url_arr['query'])?'?'.$url_arr['query']:'';
$method = 'GET'; //默认get方式
if(!empty($post_data)) $method = 'POST';
$header = "$method $getPath HTTP/1.1\r\n";
$header .= "Host: ".$url_arr['host']."\r\n"; if(!empty($cookie)){ //传递cookie信息
$_cookie = strval(NULL);
foreach($cookie AS $k=>$v){
$_cookie .= $k."=".$v.";";
}
$cookie_str = "Cookie:".base64_encode($_cookie)."\r\n";
$header .= $cookie_str;
} if(!empty($post_data)){ //传递post数据
$_post = array();
foreach($post_data AS $_k=>$_v){
$_post[] = $_k."=".urlencode($_v);
}
$_post = implode('&', $_post);
$post_str = "Content-Type:application/x-www-form-urlencoded; charset=UTF-8\r\n";
$post_str .= "Content-Length: ".strlen($_post)."\r\n"; //数据长度
$post_str .= "Connection:Close\r\n\r\n";
$post_str .= $_post; //传递post数据
$header .= $post_str;
}else{
$header .= "Connection:Close\r\n\r\n";
}
fwrite($fp, $header);
usleep(1000); // 这一句也是关键,如果没有这延时,可能在nginx服务器上就无法执行成功
fclose($fp);
return true;
} //$url = 'http://xxx.com/1.php';
$url = 'http://localhost/1.php';
$res = asyncRequest($url);
var_dump($res);
echo "我没有等a站点返回值,我就执行了";

1.php

sleep(3);
file_put_contents("1234.txt",time(),FILE_APPEND);

这段代码:

本地不用等待,并且1.php可以请求带 (本地环境win7+php7.1.13和apache (CGI/FastCGI))

但是放到服务端:不用等待,但是1.php不能被请求到。(服务器环境win2016+php7.1.13和apache (CGI/FastCGI),宝塔集成的环境)

奇怪的是,本地 asyncRequest() 本地1.php,成功,  本地asyncRequest()服务器的1.php,不成功

通过上面的描述,可以判断,应该请求可以发送到服务器,但是服务器没有处理或没有处理成功,没有响应.

(其实最终处理方法,要看服务器请求日志,看请求返回码对应处理,当时没想到~)

通过查资料:

1.fsockopen,可以设置阻塞和非阻塞请求 https://www.php.net/manual/zh/function.fsockopen.php

设置: stream_set_blocking()

2.一种可能性是: FastCGI 客户端中断时,服务器会立马停止处理,(其实这种情况在日志里面http的状态是499(client has closed connection))

http://www.webyang.net/Html/web/article_281.html

需要,设置异步程序,客户端断开继续执行,和超时时间, 好像nginx+php-fpm要配置 fastcgi_ignore_client_abort on

ignore_user_abort (true);
set_time_limit (30);

3.这个错误是我看日志的到的,我发现fsockopen请求的网页,http状态码是400错误,查了下400是请求头错误,那应该是asyncRequest()函数封装http头信息错误,(为什么本地环境没报400错误,服务器环境报400错误),我也没搞清,估计配置不一样

我例子里面的 GET  /1.php   HTTP/1.1 都是两个空格

最后删了多余的空格,改了请求头好了!!!

查的一些参考:

https://www.awaimai.com/660.html

https://blog.csdn.net/weixin_33690367/article/details/91689736

https://zhidao.baidu.com/question/2267107086723350868.html

https://segmentfault.com/q/1010000012574466/a-1020000012583303

http://www.webyang.net/Html/web/article_281.html

最新文章

  1. jQuery--事件总结
  2. tableView异步下载图片/SDWebImage图片缓存原理
  3. 微型orm fluentdata
  4. GetStartedWithWin10Develop
  5. 《TCP/IP详解卷1:协议》第19章 TCP的交互数据流-读书笔记
  6. Graph database_neo4j 底层存储结构分析(8)
  7. Oracle VM VirtualBox虚拟机安装系统
  8. 【存储器相关】RAM、SRAM、SDRAM、ROM、EPROM、EEPROM、Flash存储器区别
  9. codevs 2822爱在心中
  10. 限制**类型物料不能输入BOM
  11. maven 搭新建成之后 无法创建 src/main/java 目录解决
  12. (NO.00003)iOS游戏简单的机器人投射游戏成形记(九)
  13. 如何查看IntelliJ IDEA的版本信息
  14. Scrapy基础01
  15. spark图解
  16. AutoCompleteTextView搭配Poi搜索实现多项选择
  17. REST-framework快速构建API--四部曲
  18. poj 1182 (带权并查集)
  19. Codeforces Round #298 (Div. 2) A. Exam 构造
  20. Swagger生成的接口需要权限验证的处理方法

热门文章

  1. p3.BTC-协议
  2. python之编码与解码、is 与==的区别
  3. javaWeb的HttpServletRequest和HttpServletResponse
  4. 2019-2020-1 20199312《Linux内核原理与分析》第十一周作业
  5. LightOJ - 1102 - Problem Makes Problem(组合数)
  6. TPCH 22条SQL语句分析
  7. TCPDUMP抓包学习
  8. web文件上传下载组件
  9. learning java 获取环境变量及系统属性
  10. zabbix sender