2017 © Pedro Peláez
 

package async-http-php

php http async client

image

jenner/async-http-php

php http async client

  • Monday, November 21, 2016
  • by huyanping
  • Repository
  • 7 Watchers
  • 29 Stars
  • 239 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 12 Forks
  • 0 Open issues
  • 7 Versions
  • 369 % Grown

The README.md

async-http-php

An Async HTTP client based on curl_mulit* which is really simple and fast.
This project is a teaching experiment and I don't suggest to use it in a production environment, even though it is stable. You can try to use guzzle., (*1)

Description

  • If you want to use ssl or something else when you request a website, you can just realize a task class and extends the AbstractTask class.
  • You can register a handler using Task object, when the response is usable the Async class will call the handler to handle the response.
  • It will also return the response if it is handled by the handler or not.
  • The longer the requests execute, the more time it will save.

example code:, (*2)

$async = new \Jenner\Http\Async();
$task = \Jenner\Http\Task::createGet("http://www.baidu.com");
$async->attach($task, "baidu");

$task2 = \Jenner\Http\Task::createGet("http://www.sina.com");
$async->attach($task2, "sina");

$task3 = \Jenner\Http\Task::createGet("http://www.qq.com");
$async->attach($task3, "qq");

/**
 * you can do something here before receive the http responses
 * eg. query data from mysql or redis.
 */

$async-start();

while(true){
    // nonblock
    if(!$async->isDone()){
        echo "I am running" . PHP_EOL;
        sleep(1);
        continue;
    }

    $result = $async->execute();
    print_r($result);
    break;
}

/**
 * or you just call execute. it will block the process until all tasks are done.
 * $result = $async->execute();
 * print_r($result);
 */

use promise:, (*3)

$async = new \Jenner\Http\Async();
$task = \Jenner\Http\Task::createGet("http://www.baidu.com");
$promise = $async->attach($task, "baidu");

$promise->then(
    function ($data) {
        echo 'success:' . var_export($data, true) . PHP_EOL;
    },
    function ($data) {
        echo 'error:' . var_export($data, true) . PHP_EOL;
    }
);

$async->execute();

Performance tests

[root@huyanping async-http-php]# php tests/performance_sync.php  
------------------------------------------
mark:[total diff]
time:55.121547937393s
memory_real:1536KB
memory_emalloc:1300.5859375KB
memory_peak_real:2304KB
memory_peak_emalloc:1898.640625KB
[root@huyanping async-http-php]# php tests/performance_async.php 
------------------------------------------
mark:[total diff]
time:4.6412570476532s
memory_real:256KB
memory_emalloc:187.7109375KB
memory_peak_real:13312KB
memory_peak_emalloc:10387.8671875KB

The Versions

21/11 2016

dev-master

9999999-dev

php http async client

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-curl *

 

The Development Requires

php async http client

21/11 2016

0.6

0.6.0.0

php http async client

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-curl *

 

The Development Requires

php async http client

02/11 2015

v0.5

0.5.0.0

php http async client

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-curl *

 

The Development Requires

php async http client

25/09 2015

v0.4

0.4.0.0

php http async client

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-mysqli *

 

The Development Requires

php async http client

24/09 2015

v0.3

0.3.0.0

php http async client

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-mysqli *

 

The Development Requires

php async http client

24/09 2015

v0.2

0.2.0.0

php http async client

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-mysqli *

 

The Development Requires

php async http client

25/08 2015

v0.1

0.1.0.0

php http async client

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-mysqli *

 

The Development Requires

php async http client