utopia-php/fetch
最新稳定版本:0.5.1
Composer 安装命令:
composer require utopia-php/fetch
包简介
A simple library that provides an interface for making HTTP Requests.
README 文档
README
Lite & fast micro PHP library that provides a convenient and flexible way to perform HTTP requests in PHP applications.
Usage
Create an instance of the Client class to perform HTTP requests. The instance methods allow for setting request options like headers, timeout, connection timeout, and more.
The fetch() method on a Client instance accepts the following parameters:
url- A String containing the URL to which the request is sent.method- A String containing the HTTP method for the request. The default method isGET.body- An array of data to send as the body of the request, which can be an associative array for form data or a JSON string.query- An associative array of query parameters.
The Response object returned by fetch() provides several methods to inspect the response:
isOk()- Checks if the response status code is within the 200-299 range.getBody()- Retrieves the response body as a string.getHeaders()- Fetches the response headers as an associative array.getStatusCode()- Gets the response status code.json()- Decodes the response body to an associative array.text()- Alias forgetBody(), returns the response body as a string.blob()- Returns the response body as a blob.
Examples
GET Request
require_once __DIR__ . '/vendor/autoload.php'; use Utopia\Fetch\Client; $client = new Client(); $url = 'https://httpbin.org/get'; $method = 'GET'; $query = ['foo' => 'bar']; // Optionally set more configurations $client ->setUserAgent('CustomUserAgent/1.0') ->setAllowRedirects(true) ->setMaxRedirects(1) ->setConnectionTimeout(10) ->setTimeout(30); $resp = $client->fetch( url: $url, method: $method, query: $query ); if ($resp->isOk()) { echo "Status Code: " . $resp->getStatusCode() . "\n"; echo "Response Headers:\n"; print_r($resp->getHeaders()); echo "Response Body:\n"; echo $resp->getBody(); } else { echo "Error: " . $resp->getStatusCode() . "\n"; }
POST Request
require_once __DIR__ . '/vendor/autoload.php'; use Utopia\Fetch\Client; $client = new Client(); $url = 'https://httpbin.org/post'; $method = 'POST'; $headers = ['Content-Type' => 'application/json']; $body = ['name' => 'John Doe']; $query = ['foo' => 'bar']; // Set request headers $client->addHeader('Content-Type', 'application/json'); $resp = $client->fetch( url: $url, method: $method, body: $body, query: $query ); print_r($resp->json());
Sending a file
require_once __DIR__ . '/vendor/autoload.php'; use Utopia\Fetch\Client; $client = new Client(); $url = 'http://localhost:8000/upload'; $method = 'POST'; // Ensure you set the appropriate Content-Type for file upload $client->addHeader('Content-Type', 'multipart/form-data'); $filePath = realpath(__DIR__ . '/tests/resources/logo.png'); // Absolute path to the file $body = ['file' => new \CURLFile($filePath, 'image/png', 'logo.png')]; $resp = $client->fetch( url: $url, method: $method, body: $body ); print_r($resp->json());
统计信息
- 总下载量: 108.58k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 1
- 依赖项目数: 7
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-10-16