PHPackages                             weilun/wlcurl - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. weilun/wlcurl

ActiveLibrary[HTTP &amp; Networking](/categories/http)

weilun/wlcurl
=============

Let PHP Api request more easy, everything is open source in my github, just take it, and use it well

1.1.3(2y ago)1261MITPHPPHP &gt;=7.3

Since Apr 11Pushed 1y ago1 watchersCompare

[ Source](https://github.com/weilun-shrimp/WLCURL)[ Packagist](https://packagist.org/packages/weilun/wlcurl)[ Docs](https://github.com/weilun-shrimp/WLCURL)[ RSS](/packages/weilun-wlcurl/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)DependenciesVersions (6)Used By (0)

WLCURL
======

[](#wlcurl)

Let PHP Api (CURL) request more easyly、clearly、liberty and modelly
讓 PHP Api (CURL) 請求更加簡單、清楚易懂、自由、模組化

- [WLCURL](#wlcurl)
    - [Installation - 安裝](#installation---%E5%AE%89%E8%A3%9D)
    - [Quick Example - 快速範例](#quick-example---%E5%BF%AB%E9%80%9F%E7%AF%84%E4%BE%8B)
    - [Construct - 構成式](#construct---%E6%A7%8B%E6%88%90%E5%BC%8F)
        - [method](#method)
        - [base\_url](#base_url)
        - [end\_point](#end_point)
        - [url\_para](#url_para)
        - [body](#body)
        - [header](#header)
        - [token](#token)
            - [token\_type](#token_type)
        - [para\_type](#para_type)
        - [opt](#opt)
    - [Execute - 執行](#execute---%E5%9F%B7%E8%A1%8C)
        - [exe](#exe)
    - [Error Handle - 錯誤處理](#error-handle---%E9%8C%AF%E8%AA%A4%E8%99%95%E7%90%86)
        - [is\_error](#is_error)
        - [is\_client\_error](#is_client_error)
        - [is\_bad\_request](#is_bad_request)
        - [is\_unauthorized](#is_unauthorized)
        - [is\_forbidden](#is_forbidden)
        - [is\_method\_not\_allow](#is_method_not_allow)
        - [is\_server\_error](#is_server_error)
    - [Get Request Result - 取得請求結果](#get-request-result---%E5%8F%96%E5%BE%97%E8%AB%8B%E6%B1%82%E7%B5%90%E6%9E%9C)
        - [getBody](#getbody)
        - [getdecodeBody](#getdecodebody)
    - [Modelly-Best Advance Practice - 模組化-最佳進階做法](#modelly-best-advance-practice---%E6%A8%A1%E7%B5%84%E5%8C%96-%E6%9C%80%E4%BD%B3%E9%80%B2%E9%9A%8E%E5%81%9A%E6%B3%95)
        - [File Structure - 檔案結構](#file-structure---%E6%AA%94%E6%A1%88%E7%B5%90%E6%A7%8B)
        - [MyApiServerApi](#myapiserverapi)
        - [TargetApi](#targetapi)

Installation - 安裝
-----------------

[](#installation---安裝)

```
$ composer require weilun/wlcurl
```

Quick Example - 快速範例
--------------------

[](#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 - 構成式
---------------

[](#construct---構成式)

### method

[](#method)

> There are already have multiple default function to help you to set curl *method*
> 已經有許多默認函式提供讓你去設置請求 curl 的*method*
>
> ```
> $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

[](#base_url)

> As we all knows, URL is the most important foundation of the CURL request. And *basic url* is the first section of the url
> 眾所周知, 網址是 curl 請求最重要的一環, 而 *basic url* 是組成網址的第一個部分
>
> ```
> $api = WLCURL::get()->base_url('https://my_api_server_url');
> ```

### end\_point

[](#end_point)

> *end\_point* is the second section of the url, reach your target node
>  &gt; *end\_point* 是組成網址的第二個部分, 觸及你的目標節點
>
> ```
> $api = WLCURL::get()->end_point('/order');
> ```
>
>
>
> If you want to add end point node, put *true* in second parameter 如果你想要壘加目標節點, 在第二個參數放上 *true*
>
> ```
> $api->end_point('/{id}', true);
> // Same as
> $api = WLCURL::get()->end_point('/order/{id}');
> ```

### url\_para

[](#url_para)

> *url\_para* is the third section of the url, pass your requirement parameter to api server
>  &gt; *url\_para* 是組成網址的第三個部分, 傳送你所需的參數給目標 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 *'?page=1&amp;page\_size=24'* string
> 這會生成像是 *'?page=1&amp;page\_size=24'* 的字串

### body

[](#body)

> Pass your requirement post field parameter to api server
> 傳送你所需的 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

[](#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 structure
> ```
>
>
>
> It will build header structure like \[*"Cache-Control: no-cache"*\]

### token

[](#token)

> ```
> $api = WLCURL::get()->token('My token');
> ```
>
>
>
> ### token\_type
>
> [](#token_type)
>
> > It will put *token\_type* value in front of token as soon as build token. Defualt value is *"Bearer"*
> > 在組建 token 參數時, 會將*token\_type*值擺在前面
> >
> > ```
> > $api = WLCURL::get()->token_type('Bearer');
> > ```
> >
> >
> >
> >
> >  If you want to set token manually
> >  如果你想手動設置 token
>
> ```
> $api = WLCURL::get()->header('Authorization', 'My token type' . 'My token');
> ```

### para\_type

[](#para_type)

> It will effect the *body* parameter formation as soon as build curl request.
> The default value is "*http*".
> The value only accept in *\["http", "json"\]*.
> If value equal *"http"*, *WLCURL* will format *body* as [build\_http\_query\_para](https://www.php.net/manual/en/function.http-build-query.php).
> If value equal *"json"*, *WLCURL* will format *body* as [json\_encode](https://www.php.net/manual/en/function.json-encode.php), and set curl header Content-Type as *"application/json"* automatically.
> 此參數會直接影響 curl 請求時*body*參數轉換的形式
> 預設值是"*http*".
> 參數容許值只在*\["http", "json"\]*裡面.
> 如果參數設為*"http"*, *WLCURL* 會將 *body*參數設為 [build\_http\_query\_para](https://www.php.net/manual/en/function.http-build-query.php).
> 如果參數設為*"json"*, *WLCURL* 會將 *body*參數設為 [json\_encode](https://www.php.net/manual/en/function.json-encode.php), 並且會自動將 curl header Content-Type 參數設為 *"application/json"*
>
> ```
> $api = WLCURL::get()->para_type('json');
>    //->header('Content-Type', 'application/json'); If value is "json", WLCURL will set this automatically
> ```

### opt

[](#opt)

> Set PHP original curl opt parameter, you can find referance in [PHP CURL setopt](https://www.php.net/manual/en/function.curl-setopt.php)
> 設置 PHP 原生 curl opt 參數, 你可以參照此處[PHP CURL setopt](https://www.php.net/manual/en/function.curl-setopt.php)
>
> ```
> $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 - 執行
------------

[](#execute---執行)

### exe

[](#exe)

> *WLCURL* will do request task as soon as you call *exe()* function, and *WLCURL* will not do anything before you call it.
>  &gt; *WLCURL* 會在執行 *exe()* 函式時執行請求任務. *WLCURL* 不會在你呼叫此函式前做任何事.
>
> ```
> $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 - 錯誤處理
-------------------

[](#error-handle---錯誤處理)

*WLCURL* is already prepare multiple function to help you handle your error. It only has meaning after *exe()*

*WLCURL* 已經準備好許多函式來幫助你處理錯誤狀況. 這只會在執行 *exe()* 後有意義

### is\_error

[](#is_error)

> check curl request result return first section http status code is in *4* or *5*
> Return type *boolean*
> 檢查 curl 請求結果回傳的 http 狀態碼第一字節是否是 *4* 或 *5*
> 回傳型態 *boolean*
>
> ```
> $api->is_error();
> ```

### is\_client\_error

[](#is_client_error)

> check curl request result return first section http status code is *4* or not
> Return type *boolean*
> 檢查 curl 請求結果回傳的 http 狀態碼第一字節是否是 *4*
> 回傳型態 *boolean*
>
> ```
> $api->is_client_error();
> ```

### is\_bad\_request

[](#is_bad_request)

> check curl request result return http status code is *400* or not
> Return type *boolean*
> 檢查 curl 請求結果回傳的 http 狀態碼是否是 *400*
> 回傳型態 *boolean*
>
> ```
> $api->is_bad_request();
> ```

### is\_unauthorized

[](#is_unauthorized)

> check curl request result return http status code is *401* or not
> Return type *boolean*
> 檢查 curl 請求結果回傳的 http 狀態碼是否是 *401*
> 回傳型態 *boolean*
>
> ```
> $api->is_unauthorized();
> ```

### is\_forbidden

[](#is_forbidden)

> check curl request result return http status code is *403* or not
> Return type *boolean*
> 檢查 curl 請求結果回傳的 http 狀態碼是否是 *403*
> 回傳型態 *boolean*
>
> ```
> $api->is_forbidden();
> ```

### is\_method\_not\_allow

[](#is_method_not_allow)

> check curl request result return http status code is *405* or not
> Return type *boolean*
> 檢查 curl 請求結果回傳的 http 狀態碼是否是 *405*
> 回傳型態 *boolean*
>
> ```
> $api->is_method_not_allow();
> ```

### is\_server\_error

[](#is_server_error)

> check curl request result return first section http status code is *5* or not
> Return type *boolean*
> 檢查 curl 請求結果回傳的 http 狀態碼第一字節是否是 *5*
> 回傳型態 *boolean*
>
> ```
> $api->is_server_error();
> ```

### get\_Http\_code

[](#get_http_code)

> Retrieve raw http status code 取得原生 http 回傳之狀態碼
>
> ```
> $api->get_Http_code();
> if ($api->get_Http_code() != 200) echo 'Do something.';
> ```

### get\_error\_msg

[](#get_error_msg)

> Retrieve the error msg from php original curl 取得 PHP 原生 curl 請求錯誤的錯誤訊息
>
> ```
> $api->get_error_msg();
> ```

### get\_info

[](#get_info)

> Retrieve full request info from PHP original curl 取得所有 PHP 原生 curl 請求的回傳訊息
>
> ```
> $api->get_info();
> ```

Get Request Result - 取得請求結果
---------------------------

[](#get-request-result---取得請求結果)

It only has meaning after *exe()*.
只有在呼叫*exe()*函式後有意義.

### getBody

[](#getbody)

> Get raw request result body.
> 取得原始請求後的 body 結果.
>
> ```
> $result = $api->getBody();
> ```

### getdecodeBody

[](#getdecodebody)

> Get request result body that after [json\_decode](https://www.php.net/manual/en/function.json-decode.php).
> 取得請求後的 [json\_decode](https://www.php.net/manual/en/function.json-decode.php) body 結果.
>
> ```
> $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);
> ```

You may want to handle error before retrieve body
你也許會想要在取得結果前做錯誤處理

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

Modelly-Best Advance Practice - 模組化-最佳進階做法
------------------------------------------

[](#modelly-best-advance-practice---模組化-最佳進階做法)

Make your own packaged curl model.
製作屬於你自己的 curl 請求模組類別

### File Structure - 檔案結構

[](#file-structure---檔案結構)

`Models`
|- MyApiServerApi (extends *WLCURL*)
|- `Order`
|-- OrderApi (extends *MyApiServerApi*)
|-- OrderProductApi (extends *MyApiServerApi*)

### MyApiServerApi

[](#myapiserverapi)

> Make your own target api server model class.
> You can set any solid required curl parameter in this model constructor. And you don't have to do this again.
> 製作你自己的目標 api 請求模組類別.
> 你可以設置任何請求前所需的目標參數在構成式裡, 你將不需要再做一次.
>
> ```
> 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);
>    }
> }
> ```

### TargetApi

[](#targetapi)

> Make your own target end point model class. And package your api function.
> 製作你自己的目標節點請求 api 模組類別. 並打包函式方便日後使用.
>
> ```
> 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();
>     }
> }
> ```
>
>
>
> You can use TargetApi (OrderApi) like
> 你可以使用 TargetApi (OrderApi) 就像
>
> ```
> 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
> ])
> ```

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance29

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~151 days

Total

4

Last Release

1043d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6f5cf5d74f663ded7dd07af0ee5db6343eca264a049bd87729c8338dd91ec8f5?d=identicon)[weilun-shrimp](/maintainers/weilun-shrimp)

---

Top Contributors

[![weilun-shrimp](https://avatars.githubusercontent.com/u/73743093?v=4)](https://github.com/weilun-shrimp "weilun-shrimp (37 commits)")

---

Tags

apicurlphp

### Embed Badge

![Health badge](/badges/weilun-wlcurl/health.svg)

```
[![Health](https://phpackages.com/badges/weilun-wlcurl/health.svg)](https://phpackages.com/packages/weilun-wlcurl)
```

###  Alternatives

[friendsofsymfony/rest-bundle

This Bundle provides various tools to rapidly develop RESTful API's with Symfony

2.8k73.3M319](/packages/friendsofsymfony-rest-bundle)[php-http/discovery

Finds and installs PSR-7, PSR-17, PSR-18 and HTTPlug implementations

1.3k309.5M1.2k](/packages/php-http-discovery)[nyholm/psr7

A fast PHP7 implementation of PSR-7

1.3k235.4M2.4k](/packages/nyholm-psr7)[pusher/pusher-php-server

Library for interacting with the Pusher REST API

1.5k94.8M293](/packages/pusher-pusher-php-server)[spatie/crawler

Crawl all internal links found on a website

2.8k16.3M52](/packages/spatie-crawler)[react/http

Event-driven, streaming HTTP client and server implementation for ReactPHP

78126.4M414](/packages/react-http)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
