jyoungblood/http-request

PHP 函数,用于处理使用 CURL 发送和接收 HTTP 请求。

1.2.0 2023-01-17 20:25 UTC

This package is auto-updated.

Last update: 2024-09-22 22:42:52 UTC


README

提供 PHP 函数,用于使用 CURL 发送和接收 HTTP 请求。这是一个典型 CURL 请求工作流程的细粒度抽象,具有可配置的选项。

安装

使用 composer 轻松安装

composer require jyoungblood/http-request
use VPHP\http;
require __DIR__ . '/vendor/autoload.php';

使用方法

http::request($url, $parameters)

向指定的 URL 发送 HTTP 请求,发送数据数组并返回原始响应。

$api_data = http::request('https://external-api.com/v3/example-response', [
  'method' => 'POST', // optional, GET by default, GET and POST supported currently
  'json_decode' => true, // optional, returns an expected JSON response as a PHP array
  'debug' => true, // optional, returns all request information from curl_getinfo()
  'headers' => [ // optional, define any custom header
    'Cache-Control' => 'no-cache',
    'Content-Type' => 'application/json',
  ],
  'data' => [ // optional, will be submitted as querystring (GET) or FormData (POST)
    'user_id' => 581146,
    'api_key' => '696719xvckvzxspigh24y1e-b'
  ]
]);

http::get($url, $parameters)

http::request 的别名,使用默认的 GET 方法。

$api_data = http::get('https://external-api.com/v3/example-response', [
  'data' => [
    'user_id' => 581146,
    'api_key' => '696719xvckvzxspigh24y1e-b'
  ]
]);

数据数组中的所有内容都将作为查询字符串提交。例如

https://external-api.com/v3/example-response?user_id=581146&api_key=696719xvckvzxspigh24y1e-b

http::post($url, $parameters)

http::request 的别名,使用 POST 方法。

$api_data = http::post('https://external-api.com/v3/example-response', [
  'data' => [
    'user_id' => 581146,
    'api_key' => '696719xvckvzxspigh24y1e-b'
  ]
]);

数据数组中的所有内容都将作为 FormData 提交。

http::json($url, $parameters)

http::request() 的别名,使用 json_decode 参数(返回预期的 JSON 响应作为 PHP 数组)

$api_data = http::json('https://external-api.com/v3/example-response', [
  'data' => [
    'user_id' => 581146,
    'api_key' => '696719xvckvzxspigh24y1e-b'
  ]
]);