php 请求github api 客户端的简单示例

感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。
经测试代码如下:

<?php
/**
 * 请求github api 客户端
 *
 * @param 
 * @arrange (编程之家) jb51.cc
 **/
// http client making a request to github api
require __DIR__.'/../vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$client = new React\Http\Client($loop);
$request = $client->request('GET','https://api.github.com/repos/react-php/react/commits');
$request->on('response',function ($response) {
    $buffer = '';

    $response->on('data',function ($data) use (&$buffer) {
        $buffer .= $data;
        echo .;
    });

    $response->on('end',function () use (&$buffer) {
        $decoded = json_decode($buffer,true);
        $latest = $decoded[0]['commit'];
        $author = $latest['author']['name'];
        $date = date('F j,Y',strtotime($latest['author']['date']));

        echo \n;
        echo Latest commit on react was done by {$author} on {$date}\n;
        echo {$latest['message']}\n;
    });
});
$request->end();
$loop->run();



/*** 来自:编程之家 jb51.cc(jb51.cc) ***/ 
?>

相关文章

文章浏览阅读8.4k次,点赞8次,收藏7次。SourceCodester Onl...
文章浏览阅读3.4k次,点赞46次,收藏51次。本文为大家介绍在...
文章浏览阅读1.1k次。- php是最优秀, 最原生的模板语言, 替代...
文章浏览阅读1.1k次,点赞18次,收藏15次。整理K8s网络相关笔...
文章浏览阅读1.2k次,点赞22次,收藏19次。此网络模型提供了...
文章浏览阅读1.1k次,点赞14次,收藏19次。当我们谈论网络安...