class async_file_get_contents extends Thread{
public $ret;
public $url;
public $finished;
public function __construct($url) {
$this->finished=false;
$this->url=$url;
}
public function run() {
$this->ret=file_get_contents($this->url);
$this->finished=true;
}
}
$afgc=new async_file_get_contents("http://example.org/file.ext");
/**
* Asynchronously execute/include a PHP file. Does not record the output of the file anywhere.
*
* @param string $filename file to execute, relative to calling script
* @param string $options (optional) arguments to pass to file via the command line
*/
function asyncInclude($filename, $options = '') {
exec("/path/to/php -f {$filename} {$options} >> /dev/null &");
}
8
使用 CURL 假设请求堕胎 CURLOPT_TIMEOUT_MS
设置 ignore_user_abort(true) 以在连接关闭后继续处理 .
使用此方法无需通过头和缓冲区实现连接处理,而且依赖于操作系统,浏览器和PHP版本
Master process
function async_curl($background_process=''){
//-------------get curl contents----------------
$ch = curl_init($background_process);
curl_setopt_array($ch, array(
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER =>true,
CURLOPT_NOSIGNAL => 1, //to timeout immediately if the value is < 1000 ms
CURLOPT_TIMEOUT_MS => 50, //The maximum number of mseconds to allow cURL functions to execute
CURLOPT_VERBOSE => 1,
CURLOPT_HEADER => 1
));
$out = curl_exec($ch);
//-------------parse curl contents----------------
//$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
//$header = substr($out, 0, $header_size);
//$body = substr($out, $header_size);
curl_close($ch);
return true;
}
async_curl('http://example.com/background_process_1.php');
use GuzzleHttp\Client;
use GuzzleHttp\Promise;
$client = new Client(['base_uri' => 'http://httpbin.org/']);
// Initiate each request but do not block
$promises = [
'image' => $client->getAsync('/image'),
'png' => $client->getAsync('/image/png'),
'jpeg' => $client->getAsync('/image/jpeg'),
'webp' => $client->getAsync('/image/webp')
];
// Wait on all of the requests to complete. Throws a ConnectException
// if any of the requests fail
$results = Promise\unwrap($promises);
// Wait for the requests to complete, even if some of them fail
$results = Promise\settle($promises)->wait();
// You can access each result using the key provided to the unwrap
// function.
echo $results['image']['value']->getHeader('Content-Length')[0]
echo $results['png']['value']->getHeader('Content-Length')[0]
<?php
$request = new cURL\Request('http://yahoo.com/');
$request->getOptions()->set(CURLOPT_RETURNTRANSFER, true);
// Specify function to be called when your request is complete
$request->addListener('complete', function (cURL\Event $event) {
$response = $event->response;
$httpCode = $response->getInfo(CURLINFO_HTTP_CODE);
$html = $response->getContent();
echo "\nDone.\n";
});
// Loop below will run as long as request is processed
$timeStart = microtime(true);
while ($request->socketPerform()) {
printf("Running time: %dms \r", (microtime(true) - $timeStart)*1000);
// Here you can do anything else, while your request is in progress
}
下面你可以看到上面例子的控制台输出 . 它将显示简单的实时时钟,指示请求运行的时间:
14
让我告诉你我的方式:)
需要在服务器上安装nodejs
(我的服务器发送1000 https get请求只需2秒)
url.php:
<?
$urls = array_fill(0, 100, 'http://google.com/blank.html');
function execinbackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
}
fwite(fopen("urls.txt","w"),implode("\n",$urls);
execinbackground("nodejs urlscript.js urls.txt");
// { do your work while get requests being executed.. }
?>
urlscript.js>
var https = require('https');
var url = require('url');
var http = require('http');
var fs = require('fs');
var dosya = process.argv[2];
var logdosya = 'log.txt';
var count=0;
http.globalAgent.maxSockets = 300;
https.globalAgent.maxSockets = 300;
setTimeout(timeout,100000); // maximum execution time (in ms)
function trim(string) {
return string.replace(/^\s*|\s*$/g, '')
}
fs.readFile(process.argv[2], 'utf8', function (err, data) {
if (err) {
throw err;
}
parcala(data);
});
function parcala(data) {
var data = data.split("\n");
count=''+data.length+'-'+data[1];
data.forEach(function (d) {
req(trim(d));
});
/*
fs.unlink(dosya, function d() {
console.log('<%s> file deleted', dosya);
});
*/
}
function req(link) {
var linkinfo = url.parse(link);
if (linkinfo.protocol == 'https:') {
var options = {
host: linkinfo.host,
port: 443,
path: linkinfo.path,
method: 'GET'
};
https.get(options, function(res) {res.on('data', function(d) {});}).on('error', function(e) {console.error(e);});
} else {
var options = {
host: linkinfo.host,
port: 80,
path: linkinfo.path,
method: 'GET'
};
http.get(options, function(res) {res.on('data', function(d) {});}).on('error', function(e) {console.error(e);});
}
}
process.on('exit', onExit);
function onExit() {
log();
}
function timeout()
{
console.log("i am too far gone");process.exit();
}
function log()
{
var fd = fs.openSync(logdosya, 'a+');
fs.writeSync(fd, dosya + '-'+count+'\n');
fs.closeSync(fd);
}
<?php
parse_str("email=myemail@ehehehahaha.com&subject=this is just a test");
$_POST['email']=$email;
$_POST['subject']=$subject;
echo HTTP_POST("http://example.com/mail.php",$_POST);***
exit;
?>
<?php
/*********HTTP POST using FSOCKOPEN **************/
// by ArbZ
function HTTP_Post($URL,$data, $referrer="") {
// parsing the given URL
$URL_Info=parse_url($URL);
// Building referrer
if($referrer=="") // if not given use this script as referrer
$referrer=$_SERVER["SCRIPT_URI"];
// making string from $data
foreach($data as $key=>$value)
$values[]="$key=".urlencode($value);
$data_string=implode("&",$values);
// Find out which port is needed - if not given use standard (=80)
if(!isset($URL_Info["port"]))
$URL_Info["port"]=80;
// building POST-request: HTTP_HEADERs
$request.="POST ".$URL_Info["path"]." HTTP/1.1\n";
$request.="Host: ".$URL_Info["host"]."\n";
$request.="Referer: $referer\n";
$request.="Content-type: application/x-www-form-urlencoded\n";
$request.="Content-length: ".strlen($data_string)."\n";
$request.="Connection: close\n";
$request.="\n";
$request.=$data_string."\n";
$fp = fsockopen($URL_Info["host"],$URL_Info["port"]);
fputs($fp, $request);
while(!feof($fp)) {
$result .= fgets($fp, 128);
}
fclose($fp); //$eco = nl2br();
function getTextBetweenTags($string, $tagname) {
$pattern = "/<$tagname ?.*>(.*)<\/$tagname>/";
preg_match($pattern, $string, $matches);
return $matches[1];
}
//STORE THE FETCHED CONTENTS to a VARIABLE, because its way better and fast...
$str = $result;
$txt = getTextBetweenTags($str, "span"); $eco = $txt; $result = explode("&",$result);
return $result[1];
<span style=background-color:LightYellow;color:blue>".trim($_GET['em'])."</span>
</pre> ";
}
</pre>
17 回答
事件延期
Event扩展非常合适 . 它是Libevent库的一个端口,专为事件驱动的I / O而设计,主要用于网络 .
我编写了一个示例HTTP客户端,它允许调度多个HTTP请求并异步运行它们 .
这是基于Event扩展的示例HTTP客户端类 .
该类允许调度多个HTTP请求,然后异步运行它们 .
http-client.php
test.php
这是服务器端的示例脚本 .
用法
样本输出
(剪裁) .
注意,该代码是为CLI SAPI中的长期处理而设计的 .
对于自定义协议,请考虑使用低级API,即buffer events,buffers . 对于SSL / TLS通信,我建议将低级API与Event的ssl context结合使用 . 例子:
SSL echo server
SSL client
尽管Libevent的HTTP API很简单,但它并不像缓冲区事件那样灵活 . 例如,HTTP API当前不支持自定义HTTP方法 . 但是可以使用低级API实现几乎任何协议 .
Ev扩展
我还在non-blocking mode中使用带有sockets的Ev扩展编写了另一个HTTP客户端的示例 . 代码比基于Event的示例稍微冗长,因为Ev是一个通用事件循环 . 它不提供特定于网络的功能,但它的_2438908_观察器能够监听封装在套接字资源中的文件描述符 .
这是基于Ev扩展名的示例HTTP客户端 .
Ev扩展实现了一个简单但功能强大的通用事件循环 . 它不提供特定于网络的观察者,但其I/O watcher可用于sockets的异步处理 .
以下代码显示了如何为并行处理调度HTTP请求 .
http-client.php
测试
假设
http://my-host.local/test.php
脚本正在打印$_GET
的转储:然后
php http-client.php
命令的输出将类似于以下内容:(修剪)
请注意,在PHP 5中,套接字扩展可能会记录
EINPROGRESS
,EAGAIN
和EWOULDBLOCK
errno
值的警告 . 可以使用关闭日志关于守则的“其余部分”
应该与网络请求并行运行的代码可以在Event timer或Ev的idle watcher的回调中执行 . 您可以通过观察上面提到的样本轻松搞清楚 . 否则,我会添加另一个例子:)
这是一个工作示例,只需运行它,然后打开storage.txt,检查神奇的结果
答案我'd previously accepted didn'工作 . 它仍在等待回应 . 这确实有效,取自How do I make an asynchronous GET request in PHP?
如果您控制要异步调用的目标(例如,您自己的“longtask.php”),则可以从该端关闭连接,并且两个脚本将并行运行 . 它的工作原理如下:
quick.php通过cURL打开longtask.php(这里没有魔法)
longtask.php关闭连接并继续(魔术!)
连接关闭后
cURL返回到quick.php
两个任务并行继续
我试过这个,它运作得很好 . 但是quick.php对于longtask.php的工作方式一无所知,除非你在进程之间创建一些通信方式 .
在执行任何其他操作之前,请在longtask.php中尝试此代码 . 它将关闭连接,但仍然继续运行(并禁止任何输出):
代码从PHP manual's user contributed notes复制并稍微改进 .
这需要php5,我把它从docs.php.net中偷走了并编辑了结尾 .
我用它来监视客户端站点上发生错误的时间,它会将数据发送给我,而不会阻止输出
你可以通过使用exec()来调用可以执行HTTP请求的东西,比如
wget
,但你必须将程序的所有输出都指向某个地方,比如文件或/ dev / null,否则PHP进程会等待输出 .如果你想完全将进程与apache线程分开,那就试试(我不确定这个,但我希望你能得到这个想法):
这不是一个好的业务,你可能想要一个像cron作业调用心跳脚本的东西,该脚本轮询一个实际的数据库事件队列来做真正的异步事件 .
使用
CURL
假设请求堕胎CURLOPT_TIMEOUT_MS
设置
ignore_user_abort(true)
以在连接关闭后继续处理 .使用此方法无需通过头和缓冲区实现连接处理,而且依赖于操作系统,浏览器和PHP版本
Master process
Background process
NB
Resources
curl timeout less than 1000ms always fails?
http://www.php.net/manual/en/function.curl-setopt.php#104597
http://php.net/manual/en/features.connection-handling.php
截至2018年,Guzzle已成为HTTP请求的事实标准库,在几个现代框架中使用 . 它是用纯PHP编写的,不需要安装任何自定义扩展 .
它可以非常好地进行异步HTTP调用,甚至pool them,例如当您需要进行100次HTTP调用,但不希望一次运行超过5次时 .
并发请求示例
见http://docs.guzzlephp.org/en/stable/quickstart.html#concurrent-requests
您可以使用此库:https://github.com/stil/curl-easy
这很简单:
下面你可以看到上面例子的控制台输出 . 它将显示简单的实时时钟,指示请求运行的时间:
让我告诉你我的方式:)
需要在服务器上安装nodejs
(我的服务器发送1000 https get请求只需2秒)
url.php:
urlscript.js>
您可以使用非阻塞套接字和PHP的pecl扩展之一:
http://php.net/event
http://php.net/libevent
http://php.net/ev
https://github.com/m4rw3r/php-libev
您可以使用库,它为您提供代码和pecl扩展之间的抽象层:https://github.com/reactphp/event-loop
您还可以使用异步http-client,基于以前的库:https://github.com/reactphp/http-client
查看其他ReactPHP库:http://reactphp.org
小心异步模型 . 我建议在youtube上观看此视频:http://www.youtube.com/watch?v=MWNcItWuKpI
swoole扩展 . https://github.com/matyhtf/swoole PHP的异步和并发网络框架 .
当我对任何页面的特定URL进行POST时,这是我自己的PHP函数....示例:***我的函数的用法...
ReactPHP异步http客户端
https://github.com/shuchkin/react-http-client
通过Composer安装
异步HTTP GET
在CLI模式下运行php
好吧,超时可以设置为毫秒,参见http://www.php.net/manual/en/function.curl-setopt http://www.php.net/manual/en/function.curl-setopt