jmathai/php-multi-curl

一个高性能的PHP库,用于异步curl HTTP调用。

资助包维护!
jmathai

dev-master 2020-12-17 07:34 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:52:53 UTC


README

高性能PHP curl包装器,用于并行HTTP调用

Build Status Scrutinizer Code Quality

内容

安装

您可以使用composer从命令行安装此库。

composer require jmathai/php-multi-curl:dev-master -v

用法

基本用法可以通过使用addUrl($url/*, $options*/)方法完成。这通过传递$options作为参数调用GET $url

<?php
  // Include Composer's autoload file if not already included.
  require '../vendor/autoload.php';

  // Instantiate the MultiCurl class.
  $mc = JMathai\PhpMultiCurl\MultiCurl::getInstance();

  // Make a call to a URL.
  $call1 = $mc->addUrl('http://slowapi.herokuapp.com/delay/2.0');
  // Make another call to a URL.
  $call2 = $mc->addUrl('http://slowapi.herokuapp.com/delay/1.0');

  // Access the response for $call2.
  // This blocks until $call2 is complete without waiting for $call1
  echo "Call 2: {$call2->response}\n";

  // Access the response for $call1.
  echo "Call 1: {$call1->response}\n";

  // Output a call sequence diagram to see how the parallel calls performed.
  echo $mc->getSequence()->renderAscii();

这是该代码的输出。

Call 2: consequatur id est
Call 1: in maiores et
(http://slowapi.herokuapp.com/delay/2.0 ::  code=200, start=1447701285.5536, end=1447701287.9512, total=2.397534)
[====================================================================================================]
(http://slowapi.herokuapp.com/delay/1.0 ::  code=200, start=1447701285.5539, end=1447701287.0871, total=1.532997)
[================================================================                                    ]

高级用法

您可能需要根据特定目的配置您的cURL调用。这包括设置调用的HTTP方法、参数、标题等。您可以使用addCurl($ch)方法,并使用PHP的任何curl_*函数配置您的curl句柄。

<?php
  $mc = JMathai\PhpMultiCurl\MultiCurl::getInstance();

  // Set up your cURL handle(s).
  $ch = curl_init('http://www.google.com');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_POST, 1);
  
  // Add your cURL calls and begin non-blocking execution.
  $call = $mc->addCurl($ch);
  
  // Access response(s) from your cURL calls.
  $code = $call->code;

您可以查看tests/example.php文件中的有效代码,并从命令行执行它。

文档

方法

addUrl

addUrl((string) $url/*, (array) $options*/)

通过传递键/值数组$options作为参数,对$url执行GET调用。此方法自动将CURLOPT_RETURNTRANSFER设置为1

$call = $mc->addUrl('https://www.google.com', array('q' => 'github'));
echo $call->response;

addCurl

addCurl((curl handle) $ch)

接受curl句柄$ch并执行它。与addUrl不同,此方法不会向cURL句柄添加任何内容。您可能需要在将句柄传递给addCurl之前自己设置CURLOPT_RETURNTRANSFER

$ch = curl_init('http://www.mocky.io/v2/5185415ba171ea3a00704eed');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');

$call = $mc->addCurl($ch);
echo $call->response;

访问响应

从您调用addUrladdCurl的那一刻开始,curl调用开始执行。执行控制立即返回到您的代码,并且直到您访问responsecode变量,都不会发生阻塞。库只会阻塞您尝试访问响应的调用,并允许长时间运行的调用继续执行,同时将控制权返回给您的代码。

response

response变量返回响应的string文本。

echo $call->response;

code

code变量返回请求的HTTP响应代码integer

echo $call->code;

headers

headers变量返回响应的HTTP头部array

var_dump($call->headers);

renderAscii

返回一个打印调用延迟和并行调用程度的详细信息的string。此方法可以通过您使用的多curl实例间接调用。

echo $mc->getSequence()->renderAscii();

作者

  • jmathai

贡献者

  • Lewis Cowles (LewisCowles1986) - 增加url的可用性,无需担心CURL,同时也提供指定附加参数的选项
  • Sam Thomson (samthomson) - 打包它