PHP code example of lanzhi / php-coroutine-http-client

1. Go to this page and download the library: Download lanzhi/php-coroutine-http-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lanzhi / php-coroutine-http-client example snippets




use lanzhi\http\Client;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Logger\ConsoleLogger;

$output = new ConsoleOutput(ConsoleOutput::VERBOSITY_VERY_VERBOSE);
$client = new Client([], new ConsoleLogger($output));

//file:get.php 将拿到的 $_GET 参数 ksort 排序之后,使用 json_encode($get, JSON_UNESCAPED_UNICODE) 转换为字符串输出
$uri = "test.com/get.php";
$query = [
    'name' => 'lanzhi',
    'sex'  => 'male',
    'age'  => 'unknown',
    'tag'  => uniqid()
];

$request = $client->get($uri, ['query'=>$query]);
$request->run();
$response = $request->getReturn();
if($response->getBody()){
    echo "response body size:", $response->getBody()->getSize(), "\n";
    ksort($query);
    echo "query: ", json_encode($query, JSON_UNESCAPED_UNICODE), "\n";
    echo "body:  ", $response->getBody()->getContents(), "\n";
}else{
    var_dump($response);
}

echo "response status: ", $response->getStatusCode(), "\n";
echo "response phrase: ", $response->getReasonPhrase(), "\n\n";