好用的PHP Curl擴展工具Guzzle

好用的PHP Curl擴展工具Guzzle插圖

項目中使用第三方接口時,我們可以用封裝好的工具—-Guzzle。

Guzzle是一個PHP的 HTTP 客戶端,用來輕而易舉地發送請求,並集成到我們的 WEB 服務上。

接口簡單:構建查詢語句、POST 請求、分流上傳下載大文件、使用 HTTP cookies、上傳JSON 數據等等。
發送同步或異步的請求均使用相同的接口。
使用 PSR-7 接口來請求、響應、分流,允許你使用其他兼容的 PSR-7 類庫與Guzzle共同開發。
抽象了底層的 HTTP 傳輸,允許你改變環境以及其他的代碼,如:對 cURL 與PHP的流或 socket 並非重度依賴,非阻塞事件循環。
中間件係統允許你創建構成客戶端行為。

Composer 安裝

composer require guzzlehttp/guzzle 

簡單的例子

創建客戶端

use GuzzleHttp\Client;

$client = new Client([
    // Base URI is used with relative requests
    'base_uri' => 'http://httpbin.org',
    // You can set any number of default request options.
    'timeout'  => 2.0,
]);

發送請求

$response = $client->get('http://httpbin.org/get');
$response = $client->delete('http://httpbin.org/delete');
$response = $client->head('http://httpbin.org/get');
$response = $client->options('http://httpbin.org/get');
$response = $client->patch('http://httpbin.org/patch');
$response = $client->post('http://httpbin.org/post');
$response = $client->put('http://httpbin.org/put');

你可以創建一個請求,一切就緒後將請求傳送給 client:

use GuzzleHttp\Psr7\Request;

$request = new Request('PUT', 'http://httpbin.org/put');
$response = $client->send($request, ['timeout' => 2]);

異步請求

$promise = $client->getAsync('http://httpbin.org/get');
$promise = $client->deleteAsync('http://httpbin.org/delete');
$promise = $client->headAsync('http://httpbin.org/get');
$promise = $client->optionsAsync('http://httpbin.org/get');
$promise = $client->patchAsync('http://httpbin.org/patch');
$promise = $client->postAsync('http://httpbin.org/post');
$promise = $client->putAsync('http://httpbin.org/put');

並發請求

use GuzzleHttp\Client;
use GuzzleHttp\Promise;

$client = new Client(['base_uri' => 'http://httpbin.org/']);

// Initiate each request but do not block
$promises = [
    'image' => $client->getAsync('/image'),
    'png'   => $client->getAsync('/image/png'),
    'jpeg'  => $client->getAsync('/image/jpeg'),
    'webp'  => $client->getAsync('/image/webp')
];

// Wait on all of the requests to complete.
$results = Promise\unwrap($promises);

// You can access each result using the key provided to the unwrap
// function.
echo $results['image']->getHeader('Content-Length');
echo $results['png']->getHeader('Content-Length');

上傳數據

//Guzzle 為上傳數據提供了一些方法。 你可以發送一個包含數據流的請求,將 body 請求參數設置成一個字符串、 fopen 返回的資源、或者一個 Psr\Http\Message\StreamInterface 的實例。

// Provide the body as a string.
$r = $client->request('POST', 'http://httpbin.org/post', [
    'body' => 'raw data'
]);

// Provide an fopen resource.
$body = fopen('/path/to/file', 'r');
$r = $client->request('POST', 'http://httpbin.org/post', ['body' => $body]);

// Use the stream_for() function to create a PSR-7 stream.
$body = \GuzzleHttp\Psr7\stream_for('hello!');
$r = $client->request('POST', 'http://httpbin.org/post', ['body' => $body]);
//上傳 JSON 數據以及設置合適的頭信息可以使用 json 請求參數這個簡單的方式:

$r = $client->request('PUT', 'http://httpbin.org/put', [
    'json' => ['foo' => 'bar']
]);

國內外服務器評測推薦www.ruidata.net

相關說明:

1、VIP會員無限製任意下載,免積分。立即前往開通>>

2、下載積分可通過日常 簽到綁定郵箱 以及 積分兌換 等途徑獲得!

3、本站資源大多存儲在雲盤,如出現鏈接失效請評論反饋,如有密碼,均為:www.ipipn.com。

4、所有站內資源僅供學習交流使用。未經原版權作者許可,禁止用於任何商業環境,否則後果自負。為尊重作者版權,請購買正版作品。

5、站內資源來源於網絡公開發表文件或網友分享,如侵犯您的權益,請聯係管理員處理。

6、本站提供的源碼、模板、軟件工具等其他資源,都不包含技術服務,請大家諒解!

7、源碼、模板等資源會隨著技術、壞境的升級而存在部分問題,還請慎重選擇。

PS.源碼均收集自網絡,如有侵犯閣下權益,請發信件至: admin@ipipn.com .


源站網 » 好用的PHP Curl擴展工具Guzzle

發表評論

讚助本站發展 維持服務器消耗

全站源碼免費下載 立刻讚助