承接 simsoft/http-client 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

simsoft/http-client

最新稳定版本:1.0.2

Composer 安装命令:

composer require simsoft/http-client

包简介

A simple CURL HTTP Client.

README 文档

README

This is a simple CURL HTTP client implementation. For advance HTTP client, suggest guzzlehttp/guzzle or symfony/http-client

  1. Installation
  2. Basic Usage
  3. Sending Request
  4. Post Request
  5. Set Headers
  6. Set CURL options
  7. Response Object
  8. Advance Usage
    1. Create Custom API Client
    2. Create Custom Response

Install

composer require simsoft/http-client

Basic Usage

require "vendor/autoload.php";

use Simsoft\HttpClient\HttpClient;

$response = (new HttpClient())
     ->withBaseUri('https://domain.com/api/endpoint')
     ->withMethod('GET')
     ->withHeaders(['Authorization' => 'Bearer secret_token'])
     ->query(['foo' => 'bar'])
     ->request();

if ($response->ok()) {
    //{"status": 200, "data": [{"name": "John Doe","gender": "m"},{"name": "Jane Doe","gender": "f"}]}
    echo $response->getAttribute('status') . PHP_EOL;
    echo $response->getAttribute('data.0.name') . PHP_EOL;
    echo $response->getAttribute('data.1.name') . PHP_EOL;
} else {
    // {"errors": {"status": 404, "title": "The resource was not found"}}
    echo $response->getAttribute('errors.status') . PHP_EOL;
    echo $response->getAttribute('errors.title') . PHP_EOL;
}

// Output:
200
John Doe
Jane Doe

Sending Requests

use Simsoft\HttpClient\HttpClient;

$client = new HttpClient();
$client->withBaseUri('https://domain.com/api/endpoint');

$response = $client->get(); // Perform GET request.
$response = $client->get(['foo' => 'bar', 'foo1' => 'bar2']); // Perform GET request with query params. ?foo=bar&foo1=bar2

$response = $client->patch(); // Perform PATCH request.
$response = $client->delete(); // Perform DELETE request.

Post requests

use Simsoft\HttpClient\HttpClient;

$client = new HttpClient();
$client->withBaseUri('https://domain.com/api/endpoint');

$response = $client->formData(['foo' => 'bar'])->post(); // Perform form-data post

$response = $client->urlEncoded(['foo' => 'bar'])->post(); // Perform x-www-form-urlencoded post

$response = $client->raw(json_encode(['foo' => 'bar']))->post(); // Perform raw content post

$response = $client->graphQL('{
   users {
    id
    name
   }
}')->post(); // Perform GraphQL post.

Set Headers

use Simsoft\HttpClient\HttpClient;

$client = new HttpClient();
$client
    ->withBaseUri('https://domain.com/api/endpoint');
    ->withHeaders([
        'Authorization' => 'Bearer {{secret_token}}',
        'Content-Type' => 'application/json',
    ]);

$response = $client->formData(['foo' => 'bar'])->post();

Set CURL options

use Simsoft\HttpClient\HttpClient;

$client = new HttpClient();
$client
    ->withBaseUri('https://domain.com/api/endpoint');
    ->withOptions([
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_SSL_VERIFYPEER => false,
    ]);

$response = $client->formData(['foo' => 'bar'])->post();

Response Object

use Simsoft\HttpClient\HttpClient;

$response = $client = new HttpClient()
               ->withBaseUri('https://domain.com/api/endpoint')
               ->post(['foo' => 'bar']);

print_r($response->getHeaders());
// output
[
    'Content-Type' => 'application/json',
    'Cache-Control' => 'no-cache',
]

echo $response->getHeaderLine('Content-Type'); // output: application/json
echo $response->getStatusCode(); // output: 200.
echo $response->getTotalTime(); // output: 0.0112 micro seconds.

if ($response->ok()) {
    echo $response->getAttribute('data');

    //{"status": 200, "data": [{"name": "John Doe","gender": "m"},{"name": "Jane Doe","gender": "f"}]}
    echo $response->getAttribute('status') . PHP_EOL;       // 200
    echo $response->getAttribute('data.0.name') . PHP_EOL;  // 'John Doe'
    echo $response->getAttribute('data.1.name') . PHP_EOL;  // 'Jane Doe'

    // output all names.
    foreach($response->getAttribute('data.*.name') as $name) {
        echo $name . PHP_EOL;
    }

} elseif ($response->hasError()) {
    echo $response->getMessage() . PHP_EOL;

    // {"errors": {"status": 404, "title": "The resource was not found"}}
    echo $response->getAttribute('errors.status') . PHP_EOL;
    echo $response->getAttribute('errors.title') . PHP_EOL;
}

License

The Simsoft HttpClient is licensed under the MIT License. See the LICENSE file for details

统计信息

  • 总下载量: 116
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-06-23