PHP code example of anxu / http-client

1. Go to this page and download the library: Download anxu/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/ */

    

anxu / http-client example snippets


$client = new Client([
    'Content-type' => 'application/json' //The global header
]);

$response = $client->get('http://xxxxx.com', [
    'key' => 'value' ////The url parameters
],[
    'key'=>'value' //Set request http headers
]);

// $response = $client->post('http://xxxxx.com', [
//     'key' => 'value'   //The url parameters
// ],json_encode([  ////Set http body
//     'id'=>1,
//     'name'=>'test'
// ]), [
//     'key'=>'value' //Set request http headers
// ]);

//Response status
var_dump($response->getStatus());
//200

//Response Headers
var_dump($response->getHeaders());
// array(2) {
//     ["Set-Cookie"]=> array(2) {
//         [0]=> string(8) "SESSIONID=d98aa7fe2dd4a8b0111374f84ca3941e",
//         [1]=> string(8) "id=941e"
//     }
//     ["Content-Type"]=> array(1) {
//         [0]=> string(9) "text/html"
//     }
// }

//The specified response header
var_dump($response->getHeader('Set-Cookie'));
//string(8) "SESSIONID=d98aa7fe2dd4a8b0111374f84ca3941e"

var_dump($response->getHeader('Set-Cookie', false));
//array(2) {
//    [0]=> string(8) "SESSIONID=d98aa7fe2dd4a8b0111374f84ca3941e",
//    [1]=> string(8) "id=941e"
//}

//Response body
echo $response->getContent();

├─src                   代码目录
│  ├─Client.php           Http Client     
│  ├─ClientRequest.php    Request类
│  └─ClientResponse.php   Response类
│ 
├─tests                   测试代码   
├─composer.json           composer.json
├─phpunit.xml.dist        测试配置
└─README.md               README.md