其他教程

其他教程

Products

当前位置:首页 > 其他教程 >

fsockopen异步请求

GG网络技术分享 2025-03-18 16:17 6


<?php

function requestBySock($data) {

$postData = http_build_query($data);

$len = strlen($postData);

$host = \'localhost\';

$path = \'/index.php\';

$errno = \'\';

$errnoStr = \'\';

$fp = fsockopen($host, 80, $errno, $errnoStr, 3);

$request = \"POST $path HTTP/1.1\\r\\n\";

$request .= \"Host:localhost\\r\\n\";

$request .= \"Content-type:application/x-www-form-urlencoded\\r\\n\";

$request .= \"Content-length:\".$len.\"\\r\\n\";

$request .= \"Connection: close\\r\\n\";

//$request .= \"Expect:\\r\\n\";

$request .= \"\\r\\n\";

$request .= $postData;

fwrite($fp, $request);//提交数据

//实现异步把下面注释掉,意思是不处理返回

$receive = \'\';

while (!feof($fp)) {

$receive .= fgets($fp, 128);

}

echo \"<br />\".$receive;

//连接主动断开时,线上proxy层没有及时把请求发给上游

usleep(10000);

fclose($fp);

}

$postData = array(

\"title\" => \"woshisocket\",

\"content\" => \"tijiao\",

);

requestBySock($postData);

?>

标签: 线上 注释

提交需求或反馈

Demand feedback