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

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

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

phico/http-client

Composer 安装命令:

composer create-project phico/http-client

包简介

Simple wrapper around the built in cURL functions built for Phico

README 文档

README

A simple http client wrapping the built in cURL functions.

Installation

Install via composer

composer require phico/http-client

Usage

Making requests

GETing remote content

Use get() to request a page, the result is stored in the response body.

$response = http()->get('https://example.com');
echo $response->body;

Send query parameters as the second argument

$response = http()->get('https://example.com/hello', [
    'cache' => false,
    'name' => 'Bob'
]);
echo $response->body;

All http methods are supported

  • GET
  • POST
  • DELETE
  • PUT
  • PATCH
  • HEAD
  • OPTIONS

Custom verbs can be used via method()

$response = http()->method('PURGE', 'https://example.com/cache');

POSTing Form data

$response = http()
    ->form([
        'user' => 'bob',
        'password' => 't0p-secret'
    ])->post('https://example.com/signin');
With a file
$response = http()
    ->form([
        'overwrite' => true,
    ])
    ->file('/path/to/file')
    ->post('https://example.com/upload');

File also accepts an optional filename and optional mime-type.

$response = http()
    ->file('/path/to/invoice.pdf', 'invoice-2024.pdf')
    ->file('/path/to/readme.txt', 'read-me.txt', 'text/plain')
    ->post('https://example.com/upload');

POSTing JSON data

$response = http()
    ->json([
        'colour' => 'Blue'
    ])
    ->put('https://example.com/widgets/123');

Responses

Every request returns a structured response containing the request made, the response cookies, headers, status code and body.

$response = http()
    ->json([
        'colour' => 'Blue'
    ])
    ->post('https://example.com/widgets/123');

echo $response->http_code; // 200, 401, 500 etc..
echo $response->body;
echo join("\n", $response->cookies);  // cookies array
echo join("\n", $response->headers); // headers array

// and weirdly
var_dump($response->request);

Error handling

Exceptions will be thrown on connection or transport errors only, your code should handle the http status codes appropriately.

try {
    $response = http()
        ->form([
            'user' => '',
            'password' => 't0p-secret'
        ])->post('https://example.com/signin');
} catch (HttpClientException $e) {

   // show exception details
   echo $e->getSummary();
   echo $e->getTraceAsString();

}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2024-07-16