ChatGPT解决这个技术问题 Extra ChatGPT

如何在 PHP 中发出异步 HTTP 请求

PHP中有没有办法进行异步HTTP调用?我不关心响应,我只想执行 file_get_contents() 之类的操作,但不要等待请求完成后再执行我的其余代码。这对于在我的应用程序中触发某种“事件”或触发长流程非常有用。

有任何想法吗?

一个函数 - 'curl_multi',在 php 文档中查找它。应该能解决你的问题
这篇文章的标题具有误导性。我来寻找类似于 Node.js 中的请求或 AJAX 请求的真正异步调用。接受的答案不是异步的(它阻塞并且不提供回调),只是一个更快的同步请求。考虑更改问题或接受的答案。
通过标头和缓冲区进行连接处理并不是万无一失的。我刚刚发布了一个独立于操作系统、浏览器或 PHP 版本的新答案
异步并不意味着您不关心响应。这只是意味着调用不会阻塞主线程的执行。异步仍然需要响应,但是可以在另一个执行线程中或稍后在事件循环中处理响应。这个问题要求一个即发即弃的请求,该请求可以是同步的或异步的,具体取决于消息传递语义,无论您关心消息顺序还是传递确认。
我认为你应该在非阻塞模式下发出这个 HTTP 请求(w/c 是你真正想要的)。因为当你调用一个资源时,你基本上想知道你是否到达了服务器(或任何原因,您只需要响应)。最好的答案确实是 fsockopen 并将流读取或写入设置为非阻塞模式。这就像电话和忘记。

B
Brent

我之前接受的答案不起作用。它仍在等待响应。这确实有效,取自 How do I make an asynchronous GET request in PHP?

function post_without_wait($url, $params)
{
    foreach ($params as $key => &$val) {
      if (is_array($val)) $val = implode(',', $val);
        $post_params[] = $key.'='.urlencode($val);
    }
    $post_string = implode('&', $post_params);

    $parts=parse_url($url);

    $fp = fsockopen($parts['host'],
        isset($parts['port'])?$parts['port']:80,
        $errno, $errstr, 30);

    $out = "POST ".$parts['path']." HTTP/1.1\r\n";
    $out.= "Host: ".$parts['host']."\r\n";
    $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
    $out.= "Content-Length: ".strlen($post_string)."\r\n";
    $out.= "Connection: Close\r\n\r\n";
    if (isset($post_string)) $out.= $post_string;

    fwrite($fp, $out);
    fclose($fp);
}

这不是异步的!特别是如果另一端的服务器停机,这段代码将挂起 30 秒(fsockopen 中的第 5 个参数)。此外,fwrite 将花费其甜蜜的时间来执行(您可以使用 stream_set_timeout($fp, $my_timeout) 进行限制。您可以做的最好的是将 fsockopen 上的低超时设置为 0.1(100 毫秒),将 $my_timeout 设置为 100 毫秒. 你冒着请求超时的风险。
我向你保证它是异步的,不需要 30 秒。这是一个超时最大值。您的设置不同导致这种效果是可行的,但这对我来说非常有用。
@UltimateBrent 代码中没有任何内容表明它是异步的。它不等待响应,但这不是异步的。如果远程服务器打开连接然后挂起,此代码将等待 30 秒,直到您达到该超时。
它似乎工作“异步”的原因是因为您在关闭它之前没有从套接字读取,所以即使服务器没有及时发出响应,它也不会挂起。然而,这绝对不是异步的。如果写入缓冲区已满(极不可能),您的脚本肯定会挂在那里。您应该考虑将标题更改为“请求网页而不等待响应”之类的内容。
这既不是异步也不是使用 curl,你怎么敢称它为 curl_post_async 甚至获得赞成票......
C
Christian Davén

如果您控制要异步调用的目标(例如您自己的“longtask.php”),您可以从该端关闭连接,两个脚本将并行运行。它是这样工作的:

quick.php 通过 cURL 打开 longtask.php(这里没有魔法) longtask.php 关闭连接并继续(神奇!) 当连接关闭时,cURL 返回到 quick.php 两个任务并行继续

我已经尝试过了,它工作得很好。但是 quick.php 不会知道 longtask.php 正在做什么,除非您在进程之间创建某种通信方式。

在执行其他任何操作之前,请在 longtask.php 中尝试此代码。它将关闭连接,但仍会继续运行(并抑制任何输出):

while(ob_get_level()) ob_end_clean();
header('Connection: close');
ignore_user_abort();
ob_start();
echo('Connection Closed');
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush();
flush();

该代码是从 PHP manual's user contributed notes 复制的,并有所改进。


这会奏效。但是,如果您使用的是 MVC 框架,则可能难以实现,因为这些框架拦截和重写调用的方式。例如,它在 CakePHP 的控制器中不起作用
对这段代码有疑问,你需要在 longtask 中执行的过程必须遵循这一行吗?谢谢。
它并不完美。尝试在您的代码后添加 while(true);。该页面将挂起,这意味着它仍在前台运行。
如何“通过 cURL 打开它”?我如何“在进程之间创建一些通信方式”?
I
Internet Friend

您可以通过使用 exec() 来调用可以执行 HTTP 请求的东西,例如 wget,但您必须将程序的所有输出定向到某个位置,例如文件或 /dev/null,否则 PHP 进程将等待对于那个输出。

如果您想将进程与 apache 线程完全分开,请尝试类似(我对此不确定,但希望您明白):

exec('bash -c "wget -O (url goes here) > /dev/null 2>&1 &"');

这不是一个好生意,您可能需要像 cron 作业这样调用心跳脚本的东西,该脚本轮询实际的数据库事件队列以执行真正的异步事件。


同样,我也做了以下事情: exec("curl $url > /dev/null &");
问题:调用 'bash -c "wget"' 而不仅仅是 'wget' 有什么好处吗?
在我的测试中,使用 exec("curl $url > /dev/null 2>&1 &"); 是这里最快的解决方案之一。它比上面“已接受”答案中的 post_without_wait() 函数(14.8 秒)快得多(100 次迭代需要 1.9 秒)。而且是单线...
使用完整路径(例如 /usr/bin/curl)使其更快
这会等到脚本完成吗?
S
Simon East

截至 2018 年,Guzzle 已成为 HTTP 请求的事实上的标准库,用于多个现代框架。它是用纯 PHP 编写的,不需要安装任何自定义扩展。

它可以很好地执行异步 HTTP 调用,甚至可以在 pool them 进行 100 个 HTTP 调用,但不想一次运行超过 5 个时。

并发请求示例

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]

请参阅http://docs.guzzlephp.org/en/stable/quickstart.html#concurrent-requests


但是,这个答案不是异步的。 apparently guzzle doesn't do that
Guzzle 需要您安装 curl。否则它是非并行的,并且它不会给你任何警告它是非并行的。
感谢@daslicious 的链接 - 是的,它似乎不是完全异步的(就像你想发送请求但不关心结果时一样),但是在该线程中的一些帖子,用户提供了一种解决方法设置一个非常低的请求超时值,它仍然允许连接时间,但不等待结果。
composer require guzzle/guzzle 为我的项目添加了 537 个文件和 250 万字节的新代码!对于 HTTP 客户端!不,谢谢。
我们的项目需要更多像@EricP 这样的人。
s
stil

您可以使用此库:https://github.com/stil/curl-easy

这很简单:

<?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
}

您可以在下面看到上述示例的控制台输出。它将显示简单的实时时钟,指示请求正在运行多少时间:

https://i.stack.imgur.com/qc3wI.gif


这应该是该问题的公认答案,因为即使它不是真正的异步,它也比公认的更好,并且所有带有 guzzle 的“异步”答案(在这里您可以在执行请求时执行操作)
接受的答案 ©
我不想在我的服务器上安装任何其他东西;我想要一个纯 PHP 版本。但是,如果涉及到那个,我怎么会安装它呢?
p
philfreo
/**
 * 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 &");
}

这不是异步的,因为 exec 在您退出或分叉您要运行的进程之前一直处于阻塞状态。
您注意到最后的 & 了吗?
那么这是否会阻止脚本,我很困惑?
@pleshy 不会。与号 (&) 表示在后台运行脚本
exec() 不是在大多数共享服务器上禁用了吗?
C
Community

使用 CURL 设置低 CURLOPT_TIMEOUT_MS 设置 ignore_user_abort(true) 来伪造请求中止,以在连接关闭后继续处理。

使用此方法,无需过于依赖操作系统、浏览器和 PHP 版本,通过标头和缓冲区实现连接处理

主进程

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');

后台进程

ignore_user_abort(true);

//do something...

注意

如果您希望 cURL 在不到一秒的时间内超时,您可以使用 CURLOPT_TIMEOUT_MS,尽管“类 Unix 系统”上存在一个错误/“功能”,如果该值小于 1000 毫秒并出现错误“导致 libcurl 立即超时” cURL 错误 (28): 已超时”。对此行为的解释是:[...] 解决方案是使用 CURLOPT_NOSIGNAL 禁用信号

资源

curl超时小于1000ms总是失败?

http://www.php.net/manual/en/function.curl-setopt.php#104597

http://php.net/manual/en/features.connection-handling.php


你如何处理连接超时(解析,dns)?当我将 timeout_ms 设置为 1 时,我总是以“4 ms 后解决超时”或类似的结果结束
我不知道,但 4 毫秒对我来说已经相当快了……我认为您无法通过更改任何 curl 设置来更快地解决问题。尝试优化目标请求也许......
好的,但是 timeout_ms=1 设置了整个请求的超时时间。因此,如果您的解析时间超过 1 毫秒,那么 curl 将超时并停止请求。我根本看不出这是如何工作的(假设解析需要> 1 ms)。
虽然它没有多大意义,但它完美无缺,并且是异步执行 PHP 的一个非常好的解决方案
T
Tony

swoole 扩展。 https://github.com/matyhtf/swoole 异步和PHP 的并发网络框架。

$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);

$client->on("connect", function($cli) {
    $cli->send("hello world\n");
});

$client->on("receive", function($cli, $data){
    echo "Receive: $data\n";
});

$client->on("error", function($cli){
    echo "connect fail\n";
});

$client->on("close", function($cli){
    echo "close\n";
});

$client->connect('127.0.0.1', 9501, 0.5);

我不想在我的服务器上安装任何其他东西;我想要一个纯 PHP 版本。但是,如果涉及到那个,我怎么会安装它呢?
u
user1031143

让我告诉你我的方式:)

需要在服务器上安装 nodejs

(我的服务器发送 1000 个 https 获取请求仅需 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 函数(如 popen/exec)。请参阅 disable_functions PHP 指令。
exec() 不是在大多数共享服务器上禁用了吗?另外,我想要一个纯 PHP 解决方案。
R
Roman Shamritskiy

您可以使用非阻塞套接字和 PHP 的 pecl 扩展之一:

http://php.net/事件

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


我不想在我的服务器上安装任何其他东西;我想要一个纯 PHP 版本。但是,如果涉及到那个,我怎么会安装它呢?
h
hanshenrik
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");

对我不起作用。是的,它可以很好地获取文件,但仍然和普通的 file_get_contents() 一样慢。
P
Panda

事件扩展

Event 扩展名非常合适。它是 Libevent 库的一个端口,专为事件驱动 I/O 设计,主要用于网络。

我编写了一个示例 HTTP 客户端,它允许调度多个 HTTP 请求并异步运行它们。

这是一个基于 Event 扩展的示例 HTTP 客户端类。

该类允许调度多个 HTTP 请求,然后异步运行它们。

http-client.php

<?php
class MyHttpClient {
  /// @var EventBase
  protected $base;
  /// @var array Instances of EventHttpConnection
  protected $connections = [];

  public function __construct() {
    $this->base = new EventBase();
  }

  /**
   * Dispatches all pending requests (events)
   *
   * @return void
   */
  public function run() {
    $this->base->dispatch();
  }

  public function __destruct() {
    // Destroy connection objects explicitly, don't wait for GC.
    // Otherwise, EventBase may be free'd earlier.
    $this->connections = null;
  }

  /**
   * @brief Adds a pending HTTP request
   *
   * @param string $address Hostname, or IP
   * @param int $port Port number
   * @param array $headers Extra HTTP headers
   * @param int $cmd A EventHttpRequest::CMD_* constant
   * @param string $resource HTTP request resource, e.g. '/page?a=b&c=d'
   *
   * @return EventHttpRequest|false
   */
  public function addRequest($address, $port, array $headers,
    $cmd = EventHttpRequest::CMD_GET, $resource = '/')
  {
    $conn = new EventHttpConnection($this->base, null, $address, $port);
    $conn->setTimeout(5);

    $req = new EventHttpRequest([$this, '_requestHandler'], $this->base);

    foreach ($headers as $k => $v) {
      $req->addHeader($k, $v, EventHttpRequest::OUTPUT_HEADER);
    }
    $req->addHeader('Host', $address, EventHttpRequest::OUTPUT_HEADER);
    $req->addHeader('Connection', 'close', EventHttpRequest::OUTPUT_HEADER);
    if ($conn->makeRequest($req, $cmd, $resource)) {
      $this->connections []= $conn;
      return $req;
    }

    return false;
  }


  /**
   * @brief Handles an HTTP request
   *
   * @param EventHttpRequest $req
   * @param mixed $unused
   *
   * @return void
   */
  public function _requestHandler($req, $unused) {
    if (is_null($req)) {
      echo "Timed out\n";
    } else {
      $response_code = $req->getResponseCode();

      if ($response_code == 0) {
        echo "Connection refused\n";
      } elseif ($response_code != 200) {
        echo "Unexpected response: $response_code\n";
      } else {
        echo "Success: $response_code\n";
        $buf = $req->getInputBuffer();
        echo "Body:\n";
        while ($s = $buf->readLine(EventBuffer::EOL_ANY)) {
          echo $s, PHP_EOL;
        }
      }
    }
  }
}


$address = "my-host.local";
$port = 80;
$headers = [ 'User-Agent' => 'My-User-Agent/1.0', ];

$client = new MyHttpClient();

// Add pending requests
for ($i = 0; $i < 10; $i++) {
  $client->addRequest($address, $port, $headers,
    EventHttpRequest::CMD_GET, '/test.php?a=' . $i);
}

// Dispatch pending requests
$client->run();

测试.php

这是服务器端的示例脚本。

<?php
echo 'GET: ', var_export($_GET, true), PHP_EOL;
echo 'User-Agent: ', $_SERVER['HTTP_USER_AGENT'] ?? '(none)', PHP_EOL;

用法

php http-client.php

样本输出

Success: 200
Body:
GET: array (
  'a' => '1',
)
User-Agent: My-User-Agent/1.0
Success: 200
Body:
GET: array (
  'a' => '0',
)
User-Agent: My-User-Agent/1.0
Success: 200
Body:
GET: array (
  'a' => '3',
)
...

(修剪。)

请注意,该代码专为 CLI SAPI 中的长期处理而设计。

对于自定义协议,请考虑使用低级 API,即 buffer eventsbuffers。对于 SSL/TLS 通信,我建议将低级 API 与 Event 的 ssl context 结合使用。例子:

SSL 回显服务器

SSL 客户端

虽然 Libevent 的 HTTP API 很简单,但它不如缓冲区事件灵活。例如,HTTP API 当前不支持自定义 HTTP 方法。但是几乎可以使用低级 API 实现任何协议。

电动车扩展

我还使用 non-blocking mode 中的 socketsEv 扩展编写了另一个 HTTP 客户端的示例。该代码比基于 Event 的示例稍微冗长,因为 Ev 是一个通用的事件循环。它不提供特定于网络的功能,但它的 EvIo 观察程序尤其能够监听封装到套接字资源中的文件描述符。

这是一个基于 Ev 扩展的示例 HTTP 客户端。

Ev 扩展实现了一个简单而强大的通用事件循环。它不提供特定于网络的观察程序,但它的 I/O watcher 可用于 sockets 的异步处理。

以下代码显示了如何安排 HTTP 请求以进行并行处理。

http-client.php

<?php
class MyHttpRequest {
  /// @var MyHttpClient
  private $http_client;
  /// @var string
  private $address;
  /// @var string HTTP resource such as /page?get=param
  private $resource;
  /// @var string HTTP method such as GET, POST etc.
  private $method;
  /// @var int
  private $service_port;
  /// @var resource Socket
  private $socket;
  /// @var double Connection timeout in seconds.
  private $timeout = 10.;
  /// @var int Chunk size in bytes for socket_recv()
  private $chunk_size = 20;
  /// @var EvTimer
  private $timeout_watcher;
  /// @var EvIo
  private $write_watcher;
  /// @var EvIo
  private $read_watcher;
  /// @var EvTimer
  private $conn_watcher;
  /// @var string buffer for incoming data
  private $buffer;
  /// @var array errors reported by sockets extension in non-blocking mode.
  private static $e_nonblocking = [
    11, // EAGAIN or EWOULDBLOCK
    115, // EINPROGRESS
  ];

  /**
   * @param MyHttpClient $client
   * @param string $host Hostname, e.g. google.co.uk
   * @param string $resource HTTP resource, e.g. /page?a=b&c=d
   * @param string $method HTTP method: GET, HEAD, POST, PUT etc.
   * @throws RuntimeException
   */
  public function __construct(MyHttpClient $client, $host, $resource, $method) {
    $this->http_client = $client;
    $this->host        = $host;
    $this->resource    = $resource;
    $this->method      = $method;

    // Get the port for the WWW service
    $this->service_port = getservbyname('www', 'tcp');

    // Get the IP address for the target host
    $this->address = gethostbyname($this->host);

    // Create a TCP/IP socket
    $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    if (!$this->socket) {
      throw new RuntimeException("socket_create() failed: reason: " .
        socket_strerror(socket_last_error()));
    }

    // Set O_NONBLOCK flag
    socket_set_nonblock($this->socket);

    $this->conn_watcher = $this->http_client->getLoop()
      ->timer(0, 0., [$this, 'connect']);
  }

  public function __destruct() {
    $this->close();
  }

  private function freeWatcher(&$w) {
    if ($w) {
      $w->stop();
      $w = null;
    }
  }

  /**
   * Deallocates all resources of the request
   */
  private function close() {
    if ($this->socket) {
      socket_close($this->socket);
      $this->socket = null;
    }

    $this->freeWatcher($this->timeout_watcher);
    $this->freeWatcher($this->read_watcher);
    $this->freeWatcher($this->write_watcher);
    $this->freeWatcher($this->conn_watcher);
  }

  /**
   * Initializes a connection on socket
   * @return bool
   */
  public function connect() {
    $loop = $this->http_client->getLoop();

    $this->timeout_watcher = $loop->timer($this->timeout, 0., [$this, '_onTimeout']);
    $this->write_watcher = $loop->io($this->socket, Ev::WRITE, [$this, '_onWritable']);

    return socket_connect($this->socket, $this->address, $this->service_port);
  }

  /**
   * Callback for timeout (EvTimer) watcher
   */
  public function _onTimeout(EvTimer $w) {
    $w->stop();
    $this->close();
  }

  /**
   * Callback which is called when the socket becomes wriable
   */
  public function _onWritable(EvIo $w) {
    $this->timeout_watcher->stop();
    $w->stop();

    $in = implode("\r\n", [
      "{$this->method} {$this->resource} HTTP/1.1",
      "Host: {$this->host}",
      'Connection: Close',
    ]) . "\r\n\r\n";

    if (!socket_write($this->socket, $in, strlen($in))) {
      trigger_error("Failed writing $in to socket", E_USER_ERROR);
      return;
    }

    $loop = $this->http_client->getLoop();
    $this->read_watcher = $loop->io($this->socket,
      Ev::READ, [$this, '_onReadable']);

    // Continue running the loop
    $loop->run();
  }

  /**
   * Callback which is called when the socket becomes readable
   */
  public function _onReadable(EvIo $w) {
    // recv() 20 bytes in non-blocking mode
    $ret = socket_recv($this->socket, $out, 20, MSG_DONTWAIT);

    if ($ret) {
      // Still have data to read. Append the read chunk to the buffer.
      $this->buffer .= $out;
    } elseif ($ret === 0) {
      // All is read
      printf("\n<<<<\n%s\n>>>>", rtrim($this->buffer));
      fflush(STDOUT);
      $w->stop();
      $this->close();
      return;
    }

    // Caught EINPROGRESS, EAGAIN, or EWOULDBLOCK
    if (in_array(socket_last_error(), static::$e_nonblocking)) {
      return;
    }

    $w->stop();
    $this->close();
  }
}

/////////////////////////////////////
class MyHttpClient {
  /// @var array Instances of MyHttpRequest
  private $requests = [];
  /// @var EvLoop
  private $loop;

  public function __construct() {
    // Each HTTP client runs its own event loop
    $this->loop = new EvLoop();
  }

  public function __destruct() {
    $this->loop->stop();
  }

  /**
   * @return EvLoop
   */
  public function getLoop() {
    return $this->loop;
  }

  /**
   * Adds a pending request
   */
  public function addRequest(MyHttpRequest $r) {
    $this->requests []= $r;
  }

  /**
   * Dispatches all pending requests
   */
  public function run() {
    $this->loop->run();
  }
}


/////////////////////////////////////
// Usage
$client = new MyHttpClient();
foreach (range(1, 10) as $i) {
  $client->addRequest(new MyHttpRequest($client, 'my-host.local', '/test.php?a=' . $i, 'GET'));
}
$client->run();

测试

假设 http://my-host.local/test.php 脚本正在打印 $_GET 的转储:

<?php
echo 'GET: ', var_export($_GET, true), PHP_EOL;

那么 php http-client.php 命令的输出将类似于以下内容:

<<<<
HTTP/1.1 200 OK
Server: nginx/1.10.1
Date: Fri, 02 Dec 2016 12:39:54 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: close
X-Powered-By: PHP/7.0.13-pl0-gentoo

1d
GET: array (
  'a' => '3',
)

0
>>>>
<<<<
HTTP/1.1 200 OK
Server: nginx/1.10.1
Date: Fri, 02 Dec 2016 12:39:54 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: close
X-Powered-By: PHP/7.0.13-pl0-gentoo

1d
GET: array (
  'a' => '2',
)

0
>>>>
...

(修剪)

请注意,在 PHP 5 中,sockets 扩展可能会记录 EINPROGRESSEAGAINEWOULDBLOCK errno 值的警告。可以使用以下命令关闭日志

error_reporting(E_ERROR);

关于守则的“其余部分”

我只想做类似 file_get_contents() 的事情,但不要等待请求完成后再执行我的其余代码。

例如,应该与网络请求并行运行的代码可以在 Event timer 或 Ev 的 idle watcher 的回调中执行。您可以通过观看上面提到的示例轻松弄清楚。否则,我将添加另一个示例:)


V
Vedmant

我发现这个包非常有用且非常简单:https://github.com/amphp/parallel-functions

<?php

use function Amp\ParallelFunctions\parallelMap;
use function Amp\Promise\wait;

$responses = wait(parallelMap([
    'https://google.com/',
    'https://github.com/',
    'https://stackoverflow.com/',
], function ($url) {
    return file_get_contents($url);
}));

它将并行加载所有 3 个 url。您还可以在闭包中使用类实例方法。

例如,我使用基于此包 https://github.com/spatie/laravel-collection-macros#parallelmap 的 Laravel 扩展

这是我的代码:

    /**
     * Get domains with all needed data
     */
    protected function getDomainsWithdata(): Collection
    {
        return $this->opensrs->getDomains()->parallelMap(function ($domain) {
            $contact = $this->opensrs->getDomainContact($domain);
            $contact['domain'] = $domain;
            return $contact;
        }, 10);
    }

它在 10 个并行线程中加载所有需要的数据,而不是 50 秒没有异步它只在 8 秒内完成。


我不想在我的服务器上安装任何其他东西;我想要一个纯 PHP 版本。但是,如果涉及到那个,我怎么会安装它呢?
@RedGuy11 composer require amphp/parallel-functions
我在哪里运行这个?
在终端(控制台)
A
AlexTR

这是一个工作示例,只需运行它并随后打开 storage.txt 即可查看神奇的结果

<?php
    function curlGet($target){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $target);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec ($ch);
        curl_close ($ch);
        return $result;
    }

    // Its the next 3 lines that do the magic
    ignore_user_abort(true);
    header("Connection: close"); header("Content-Length: 0");
    echo str_repeat("s", 100000); flush();

    $i = $_GET['i'];
    if(!is_numeric($i)) $i = 1;
    if($i > 4) exit;
    if($i == 1) file_put_contents('storage.txt', '');

    file_put_contents('storage.txt', file_get_contents('storage.txt') . time() . "\n");

    sleep(5);
    curlGet($_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] . '?i=' . ($i + 1));
    curlGet($_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] . '?i=' . ($i + 1));

你确定这是异步的吗?它看起来不像...
j
jmarkmurphy

当我向任何页面的特定 URL 进行 POST 时,这是我自己的 PHP 函数......示例:*** 我的函数的使用......

    <?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>

我不想在我的服务器上安装任何其他东西;我想要一个纯 PHP 版本。但是,如果涉及到那个,我怎么会安装它呢?
S
Sergey Shuchkin

ReactPHP 异步 http 客户端
https://github.com/shuchkin/react-http-client

通过 Composer 安装

$ composer require shuchkin/react-http-client

异步 HTTP GET

// get.php
$loop = \React\EventLoop\Factory::create();

$http = new \Shuchkin\ReactHTTP\Client( $loop );

$http->get( 'https://tools.ietf.org/rfc/rfc2068.txt' )->then(
    function( $content ) {
        echo $content;
    },
    function ( \Exception $ex ) {
        echo 'HTTP error '.$ex->getCode().' '.$ex->getMessage();
    }
);

$loop->run();

在 CLI 模式下运行 php

$ php get.php

我不想在我的服务器上安装任何其他东西;我想要一个纯 PHP 版本。但是,如果涉及到那个,我怎么会安装它呢?
n
nacholibre

Symfony HttpClient 是异步的 https://symfony.com/doc/current/components/http_client.html

例如你可以

use Symfony\Component\HttpClient\HttpClient;

$client = HttpClient::create();
$response1 = $client->request('GET', 'https://website1');
$response2 = $client->request('GET', 'https://website1');
$response3 = $client->request('GET', 'https://website1');
//these 3 calls with return immediately
//but the requests will fire to the website1 webserver

$response1->getContent(); //this will block until content is fetched
$response2->getContent(); //same 
$response3->getContent(); //same

我不想在我的服务器上安装任何其他东西;我想要一个纯 PHP 版本。但是,如果涉及到那个,我怎么会安装它呢?
这是纯 php,但您需要启用 curl php 扩展才能工作。
嗯好的。我只是用 curl_multi 寿
C
Commercial Suicide

嗯,超时时间可以设置为毫秒,见 http://www.php.net/manual/en/function.curl-setopt 中的“CURLOPT_CONNECTTIMEOUT_MS”


它只设置了一个上限,认为超时。它根本不是异步的。
你确定这是异步的吗?它看起来不像...