WLCURL
Let PHP Api (CURL) request more easyly、clearly、liberty and modelly 讓 PHP Api (CURL) 請求更加簡單、清楚易懂、自由、模組化
Install / Use
/learn @weilun-shrimp/WLCURLREADME
WLCURL
Let PHP Api (CURL) request more easyly、clearly、liberty and modelly<br> 讓 PHP Api (CURL) 請求更加簡單、清楚易懂、自由、模組化
- WLCURL
Installation - 安裝
$ composer require weilun/wlcurl
Quick Example - 快速範例
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();Same as - 如下同上
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();Same as - 如下同上
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();Same as above but modelly - 如下同上但模組化
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] ]);Reach result and error handle
if ($api->is_error()) throw new \Exception('Somethong go wrong.'); $result = $api->getBody(); // fetch result
Construct - 構成式
method
There are already have multiple default function to help you to set curl <font color=red>method</font><br> 已經有許多默認函式提供讓你去設置請求 curl 的<font color=red>method</font>
$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 = []);Customize method - 客製方法
$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 = []);
base_url
As we all knows, URL is the most important foundation of the CURL request. And <font color=red>basic url</font> is the first section of the url<br> 眾所周知, 網址是 curl 請求最重要的一環, 而 <font color=red>basic url</font> 是組成網址的第一個部分
$api = WLCURL::get()->base_url('https://my_api_server_url');
end_point
<font color=red>end_point</font> is the second section of the url, reach your target node<br> > <font color=red>end_point</font> 是組成網址的第二個部分, 觸及你的目標節點
$api = WLCURL::get()->end_point('/order');If you want to add end point node, put <font color=blue>true</font> in second parameter 如果你想要壘加目標節點, 在第二個參數放上 <font color=blue>true</font>
$api->end_point('/{id}', true); // Same as $api = WLCURL::get()->end_point('/order/{id}');
url_para
<font color=red>url_para</font> is the third section of the url, pass your requirement parameter to api server<br> > <font color=red>url_para</font> 是組成網址的第三個部分, 傳送你所需的參數給目標 api 伺服器
$api = WLCURL::get() ->url_para('page', 1); ->url_para('page_size', 24); // Same as $api = WLCURL::get()->url_para([ 'page' => 1, 'page_size' => 24 ]);It will generate like <font color=red>'?page=1&page_size=24'</font> string<br> 這會生成像是 <font color=red>'?page=1&page_size=24'</font> 的字串
body
Pass your requirement post field parameter to api server<br> 傳送你所需的 post field 參數給目標 api 伺服器
$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' ]);
header
$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 structureIt will build header structure like <font color=red>["Cache-Control: no-cache"]</font>
token
$api = WLCURL::get()->token('My token');token_type
It will put <font color=red>token_type</font> value in front of token as soon as build token. Defualt value is <font color=blue>"Bearer"</font><br> 在組建 token 參數時, 會將<font color=red>token_type</font>值擺在前面
$api = WLCURL::get()->token_type('Bearer');<br> If you want to set token manually <br> 如果你想手動設置 token
$api = WLCURL::get()->header('Authorization', 'My token type' . 'My token');
para_type
It will effect the <font color=red>body</font> parameter formation as soon as build curl request.<br> The default value is <font color=blue>"http"</font>.<br> The value only accept in <font color=blue>["http", "json"]</font>.<br> If value equal <font color=blue>"http"</font>, <font color=red>WLCURL</font> will format <font color=red>body</font> as build_http_query_para. <br> If value equal <font color=blue>"json"</font>, <font color=red>WLCURL</font> will format <font color=red>body</font> as json_encode, and set curl header Content-Type as <font color=blue>"application/json"</font> automatically. <br> 此參數會直接影響 curl 請求時<font color=red>body</font>參數轉換的形式<br> 預設值是<font color=blue>"http"</font>.<br> 參數容許值只在<font color=blue>["http", "json"]</font>裡面.<br> 如果參數設為<font color=blue>"http"</font>, <font color=red>WLCURL</font> 會將 <font color=red>body</font>參數設為 build_http_query_para. <br> 如果參數設為<font color=blue>"json"</font>, <font color=red>WLCURL</font> 會將 <font color=red>body</font>參數設為 json_encode, 並且會自動將 curl header Content-Type 參數設為 <font color=blue>"application/json"</font><br>
$api = WLCURL::get()->para_type('json'); //->header('Content-Type', 'application/json'); If value is "json", WLCURL will set this automatically
opt
Set PHP original curl opt parameter, you can find referance in PHP CURL setopt<br> 設置 PHP 原生 curl opt 參數, 你可以參照此處PHP CURL setopt
$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.
Execute - 執行
exe
<font color=red>WLCURL</font> will do request task as soon as you call <font color=red>exe()</font> function, and <font color=red>WLCURL</font> will not do anything before you call it.<br> > <font color=red>WLCURL</font> 會在執行 <font color=red>exe()</font> 函式時執行請求任務. <font color=red>WLCURL</font> 不會在你呼叫此函式前做任何事.
$api = (new WLCURL) // Default GET method ->base_url('https://my_api_server_url'); ->end_point('/order'); ->url_para(['page' => 1, 'page_size' => 24]); ->exe();
Error Handle - 錯誤處理
<font color=red>WLCURL</font> is already prepare multiple function to help you handle your error. It only has meaning after <font color=red>exe()</font><br>
<font color=red>WLCURL</font> 已經準備好許多函式來幫助你處理錯誤狀況. 這只會在執行 <font color=red>exe()</font> 後有意義
is_error
check curl request result return first section http status code is in <font color=blue>4</font> or <font color=blue>5</font><br> Return type <font color=red>boolean</font><br> 檢查 curl 請求結果回傳的 http 狀態碼第一字節是否是 <font color=blue>4</font> 或 <font color=blue>5</font><br> 回傳型態 <font color=red>boolean<
