软件vampcurl-easy

关于此软件包的最新版本(v1.1.2)没有可用的许可信息。

v1.1.2 2013-05-30 18:26 UTC

This package is not auto-updated.

Last update: 2024-09-24 00:37:56 UTC


README

## 目录

## 简介 ### 描述 这是一个小巧但强大且健壮的库,可以加快处理速度。如果您厌倦了使用PHP cURL扩展及其过程式接口,但又想保持对脚本执行的掌控 - 这是您的最佳选择!### 主要特性

  • 广泛进行单元测试。
  • 轻量级库,接口水平适中。它不是一站式库。
  • 具有非常简单接口的并行/异步连接。
  • 在运行时并行/附加/分离请求!
  • 支持回调,您可以控制执行过程。
  • 智能设置器作为CURLOPT_*常量的替代品。
  • 如果您了解cURL php扩展,您无需从头开始学习

## 安装 为了使用cURL-PHP库,您需要安装“libcurl”软件包。它还需要PHP 5.3或更高版本以及Symfony的EventDispatcher 2.1.*或更高版本。

建议使用Composer进行安装。

{
    "require": {
        "stil/curl-easy": "*"
    }
}

## 示例 ### 使用阻塞的单个请求

<?php
// We will download info about YouTube video: http://youtu.be/_PsdGQ96ah4
$request = new \cURL\Request('http://gdata.youtube.com/feeds/api/videos/_PsdGQ96ah4?v=2&alt=json');
$request->getOptions()
	->set(CURLOPT_TIMEOUT, 5)
	->set(CURLOPT_RETURNTRANSFER, true);
$response = $request->send();
$feed = json_decode($response->getContent(), true);
echo $feed['entry']['title']['$t'];

上述示例将输出:Karmah - Just be good to me### 无阻塞的单个请求

<?php
// We will download info about YouTube video: http://youtu.be/_PsdGQ96ah4
$request = new \cURL\Request('http://gdata.youtube.com/feeds/api/videos/_PsdGQ96ah4?v=2&alt=json');
$request->getOptions()
	->set(CURLOPT_TIMEOUT, 5)
	->set(CURLOPT_RETURNTRANSFER, true);
$request->addListener('complete', function (\cURL\Event $event) {
    $response = $event->response;
    $feed = json_decode($response->getContent(), true);
    echo $feed['entry']['title']['$t'];
});


while ($request->socketPerform()) {
    // do anything else when the requests are processed
    $request->socketSelect();
    // line below pauses execution until there's new data on socket
}

上述示例将输出:Karmah - Just be good to me### 并行请求

<?php
// We will download info about 2 YouTube videos:
// http://youtu.be/XmSdTa9kaiQ and
// http://youtu.be/6dC-sm5SWiU

// Init queue of requests
$queue = new \cURL\RequestsQueue;
// Set default options for all requests in queue
$queue->getDefaultOptions()
	->set(CURLOPT_TIMEOUT, 5)
	->set(CURLOPT_RETURNTRANSFER, true);
// Set function to be executed when request will be completed
$queue->addListener('complete', function (\cURL\Event $event) {
    $response = $event->response;
	$json = $response->getContent(); // Returns content of response
    $feed = json_decode($json, true);
    echo $feed['entry']['title']['$t'] . "\n";
});

$request = new \cURL\Request('http://gdata.youtube.com/feeds/api/videos/XmSdTa9kaiQ?v=2&alt=json');
// Add request to queue
$queue->attach($request);

$request = new \cURL\Request('http://gdata.youtube.com/feeds/api/videos/6dC-sm5SWiU?v=2&alt=json');
$queue->attach($request);

// Execute queue
$queue->send();

上述示例将输出

Kool & The Gang - Fresh - 2004
U2 - With Or Without You

### 并行非阻塞请求

<?php
// We will download info about 2 YouTube videos:
// http://youtu.be/XmSdTa9kaiQ and
// http://youtu.be/6dC-sm5SWiU

// Init queue of requests
$queue = new \cURL\RequestsQueue;
// Set default options for all requests in queue
$queue->getDefaultOptions()
	->set(CURLOPT_TIMEOUT, 5)
	->set(CURLOPT_RETURNTRANSFER, true);
// Set function to be executed when request will be completed
$queue->addListener('complete', function (\cURL\Event $event) {
    $response = $event->response;
	$json = $response->getContent(); // Returns content of response
    $feed = json_decode($json, true);
    echo $feed['entry']['title']['$t'] . "\n";
});

$request = new \cURL\Request('http://gdata.youtube.com/feeds/api/videos/XmSdTa9kaiQ?v=2&alt=json');
// Add request to queue
$queue->attach($request);

$request = new \cURL\Request('http://gdata.youtube.com/feeds/api/videos/6dC-sm5SWiU?v=2&alt=json');
$queue->attach($request);

// Execute queue
while ($queue->socketPerform()) {
    echo '*';
    $queue->socketSelect();
}

上述示例将输出类似的内容

***Kool & The Gang - Fresh - 2004
**U2 - With Or Without You

### 在运行时添加新请求

$requests = array();
$videos = array('tv0IEwypXkY', 'p8EH1_jZBl4', 'pXxwxEb3akc', 'Fh-O6nvQr9Q', '31vXOeV67PQ');
foreach ($videos as $id) {
    $requests[] = new \cURL\Request('http://gdata.youtube.com/feeds/api/videos/'.$id.'?v=2&alt=json');
}

$queue = new \cURL\RequestsQueue;
$queue
    ->getDefaultOptions()
    ->set(CURLOPT_RETURNTRANSFER, true);

$queue->addListener('complete', function (\cURL\Event $event) use (&$requests) {
    $response = $event->response;
    $json = $response->getContent(); // Returns content of response
    $feed = json_decode($json, true);
    echo $feed['entry']['title']['$t'] . "\n";
    
    $next = array_pop($requests);
    if ($next) {
        $event->queue->attach($next);
    }
});

$queue->attach(array_pop($requests));
$queue->send();

上述示例将输出类似的内容

Kid Cudi - Cudi Zone
Kid Cudi-I Be High
Kid Cudi - Marijuana
Kid Cudi - Trapped In My Mind (HQ)
KiD Cudi - Don't Play This Song **LYRICS** [ Man On The Moon II ]

### 智能选项设置 将CURLOPT_*替换为set*(),您将收到方法名称。示例

$opts = new \cURL\Options;

$opts->set(CURLOPT_URL, $url);
// it is equivalent to
// $opts->setUrl($url);

$opts->set(CURLOPT_RETURNTRANSFER, true);
// it is equivalent to
// $opts->setReturnTransfer(true);
// or
// $opts->setReTURNTranSFER(true);
// character case does not matter

$opts->set(CURLOPT_TIMEOUT, 5);
// it is equivalent to
// $opts->setTimeout(5);

// then you can assign options to Request

$request = new \cURL\Request;
$request->setOptions($opts);

// or make it default in RequestsQueue

$queue = new \cURL\RequestsQueue;
$queue->setDefaultOptions($opts);

### 错误处理 您可以在Response类中访问cURL错误代码。示例

$request = new \cURL\Request('http://non-existsing-page/');
$response = $request->send();

if ($response->hasError()) {
    $error = $response->getError();
    echo
        'Error code: '.$error->getCode()."\n".
        'Message: "'.$error->getMessage().'"';
}

可能上述示例将输出

Error code: 6
Message: "Could not resolve host: non-existsing-page; Host not found"

您可以在这里找到所有CURLE_*错误代码。##cURL\Request ###Request::__construct ###Request::getOptions ###Request::setOptions ###RequestsQueue::socketPerform ###RequestsQueue::socketSelect ###Request::send ##cURL\RequesstQueue ###RequestsQueue::__construct ###RequestsQueue::getDefaultOptions ###RequestsQueue::setDefaultOptions ###RequestsQueue::socketPerform ###RequestsQueue::socketSelect ###RequestsQueue::send ##cURL\Response ###Response::getContent ###Response::getInfo ###Response::hasError ###Response::getError ##cURL\Options ###Options::set ###Options::toArray ##cURL\Error ###Error::getCode ###Error::getMessage