PHP code example of weilun / wlcurl

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

    

weilun / wlcurl example snippets


> use WeiLun/WLCURL;
>
> $order_api = new WLCURL; // Default GET method
> $order_api->base_url('https://my_api_server_url');
> $order_api->end_point('/order');
> $order_api->url_para(['page' => 1, 'page_size' => 24]);
> $order_api->exe();
> 

> use WeiLun/WLCURL;
>
> $order_api = (new WLCURL) // Default GET method
>   ->base_url('https://my_api_server_url');
>   ->end_point('/order');
>   ->url_para(['page' => 1, 'page_size' => 24]);
>   ->exe();
> 

> use WeiLun/WLCURL;
>
> $order_api = WLCURL::get([
>    'base_url' => 'https://my_api_server_url',
>    'end_point' => '/order',
>    'url_para' => ['page' => 1, 'page_size' => 24]
> ])->exe();
> 

> use WeiLun/WLCURL;
>
> class MyApiServerApi extends WLCURL {
>    function __construct($para) {
>       $this->base_url = 'https://my_api_server_url';
>       parent::__construct($para);
>    }
> }
>
> class OrderApi extends MyApiServerApi {
>    function __construct($para) {
>       $this->end_point = '/order';
>       parent::__construct($para);
>    }
>
>    public static function fetch_index(array $url_para = []) {
>       return (new self([
>          'url_para' => $url_para
>       ]))->exe();
>    }
> }
>
> $api = OrderApi::fetch_index([
>    'url_para' => ['page' => 1, 'page_size' => 24]
> ]);
> 

> if ($api->is_error()) throw new \Exception('Somethong go wrong.');
> $result = $api->getBody(); // fetch result
> 

> $api = new WLCURL(array $my_construct_para = []); // Default GET method
> $api = WLCURL::get(array $my_construct_para = []);
> $api = WLCURL::post(array $my_construct_para = []);
> $api = WLCURL::put(array $my_construct_para = []);
> $api = WLCURL::patch(array $my_construct_para = []);
> $api = WLCURL::delete(array $my_construct_para = []);
> 

> > $api = new WLCURL;
> > $api->method = 'My custom method';
> > // Same as above
> > $api = new WLCURL(['method' => 'My custom method']);
> > $api = WLCURL::request('My custom method', array $my_construct_para = []);
> >
> > 

> $api = WLCURL::get()->base_url('https://my_api_server_url');
> 

> $api = WLCURL::get()->end_point('/order');
> 

> $api->end_point('/{id}', true);
> // Same as
> $api = WLCURL::get()->end_point('/order/{id}');
> 

> $api = WLCURL::get()
>   ->url_para('page', 1);
>   ->url_para('page_size', 24);
> // Same as
> $api = WLCURL::get()->url_para([
>    'page' => 1,
>    'page_size' => 24
> ]);
> 

> $api = WLCURL::post()
>   ->body('title', 'My title'); // Add parameter in body structure
>   ->body('content', 'My content');
> // Same as
> $api = WLCURL::post()->body([ // Replace whole body structure
>    'title' => 'My title',
>    'content' => 'My content'
> ]);
> 

> $api = WLCURL::get()->header('Cache-Control', 'no-cache'); // Add parameter
> // Same as
> $api = WLCURL::get()->header(['Cache-Control' => 'no-cache']); // Add parameter
> // Same as
> $api = WLCURL::get()->opt(CURLOPT_HTTPHEADER, ["Cache-Control: no-cache"]); // Replace whole curl header structure
> 

> $api = WLCURL::get()->token('My token');
> 

> > $api = WLCURL::get()->token_type('Bearer');
> > 

> $api = WLCURL::get()->header('Authorization', 'My token type' . 'My token');
> 

> $api = WLCURL::get()->para_type('json');
>    //->header('Content-Type', 'application/json'); If value is "json", WLCURL will set this automatically
> 

> $api = WLCURL::get()->opt(CURLOPT_RETURNTRANSFER, true); // Add parameter
> // Same as
> $api = WLCURL::get()->header([CURLOPT_RETURNTRANSFER => true]); // Add parameter
> // Same as
> $api = WLCURL::get();
> $api->opt = [CURLOPT_HTTPHEADER => true]; // Replace whole curl opt structure, It's dangerous, please becareful.
> 

> $api = (new WLCURL) // Default GET method
>    ->base_url('https://my_api_server_url');
>    ->end_point('/order');
>    ->url_para(['page' => 1, 'page_size' => 24]);
>    ->exe();
> 

> $api->is_error();
> 

> $api->is_client_error();
> 

> $api->is_bad_request();
> 

> $api->is_unauthorized();
> 

> $api->is_forbidden();
> 

> $api->is_method_not_allow();
> 

> $api->is_server_error();
> 

> $api->get_Http_code();
> if ($api->get_Http_code() != 200) echo 'Do something.';
> 

> $api->get_error_msg();
> 

> $api->get_info();
> 

> $result = $api->getBody();
> 

> $result = $api->getdecodeBody();
> //Same as
> $result = json_decode($api->getBody());
> //There have three option parameter to config decode result same as php json_decode()
> $result = $api->getdecodeBody($associative = null, int $depth = 512, int $flags = 0);
> 

if ($api->is_error()) throw new \Exception('Somethong go wrong.');
$result = $api->getBody();

> namespace App\Models;
>
> use WeiLun\WLCURL;
>
> class MyApiServerApi extends WLCURL {
>     function __construct($para) {
>         $this->base_url('https://my_api_server_url');
>         $this->token('My Api Server Token.');
>         $this->para_type('json');
>         parent::__construct($para);
>    }
> }
> 

> namespace App\Models\Order;
>
> use App\Models\MyApiServerApi;
>
> class OrderApi extends MyApiServerApi {
>     function __construct($para) {
>         $this->end_point('/order');
>         parent::__construct($para);
>     }
>
>     public static function index(int $page = 1, int $pae_size = 24) {
>         return self::get([
>             'url_para' => [
>                 'page' => $page,
>                 'page_size' => $page_size
>             ]
>         ])->exe();
>     }
>
>     public static function retrieve(int $id) {
>         $self = self::get();
>         $self->end_point("/$id", true); // Make end_point become /order/{id}
>         return $self->exe();
>     }
>
>     public static function create(array $body) {
>         return self::post([
>             'body' => $body
>         ])->exe();
>     }
>
>     public static function update(int $id, array $body) {
>         $self = self::put();
>         $self->end_point("/$id", true); // Make end_point become /order/{id}
>         $self->body($body);
>         return $self->exe();
>     }
> }
> 

> use App\Models\Order\OrderApi;
>
> $order_index_api = OrderApi::index(1, 24);
> $order_api = OrderApi::retrieve(1);
> $order_api = OrderApi::create([
>     'customer_name' => 'My customer name',
>     'total' => 100
> ]);
> $order_update_api = OrderApi::update(1, [
>     'customer_name' => 'Changed customer name',
>     'total' => 10
> ])
>