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();
>
> $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();
> }
> }
>