cvsouth/http

使用纯PHP进行简单的HTTP请求

1.0.6 2021-02-16 19:46 UTC

This package is auto-updated.

Last update: 2021-07-24 12:56:40 UTC


README

本软件包提供纯PHP的HTTP请求,不依赖于任何额外的依赖项,如ext-curl或Guzzle之类的过度设计解决方案,并且也使得异常处理变得简单。

安装

composer require cvsouth/http

用法

基本请求

GET

该软件包使基本HTTP请求变得极其简单

$response = http_get($url);

POST

对于POST数据,您可以传递原始字符串数据或关联数组以编码为表单数据

$response = http_post($url, 'data');
// or
$response = http_post($url, ['key' => 'value']);

流式响应

GET

$stream = http_get_stream($url);

// do something with the stream...

POST

$stream = http_post_stream($url, ['key' => 'value']);

// do something with the stream...

指定额外的头部

您可以通过传递关联数组作为下一个参数来指定额外的请求头部

GET

$response = http_get($url, ['Pragma' => 'no-cache']);
$stream = http_get_stream($url, ['Pragma' => 'no-cache']);

POST

$response = http_post($url, ['key' => 'value'], ['Pragma' => 'no-cache']);
$stream = http_post_stream($url, ['key' => 'value'], ['Pragma' => 'no-cache']);

读取响应头部

您可以通过关联数组中的键值对作为引用参数来访问响应头部

GET

$response = http_get($url, null, $response_headers);

print_r($response_headers);
$stream = http_get_stream($url, null, $response_headers);

print_r($response_headers);

// do something with the stream...

POST

$response = http_post($url, ['key' => 'value'], null, $response_headers);

print_r($response_headers);
$stream = http_post_stream($url, ['key' => 'value'], null, $response_headers);

print_r($response_headers);

// do something with the stream...

访问特定头部

您也可以使用 http_response_header 来访问特定的响应头部。第一个参数接受头部名称(不区分大小写)。第二个参数是头部数组的数组,或者您可以传递一个流

$response = http_get($url, null, $response_headers);

$content_type = http_response_header('Content-Type', $response_headers);
$response = http_get_stream($url);

$content_type = http_response_header('Content-Type', $stream);

// do something with stream...

响应下载

除了使用此软件包从HTTP端点读取数据外,我们还有一些辅助函数用于返回我们自己的响应。 http_download_headers 可以用来设置响应头部,以强制用户浏览器下载。该函数接受三个参数;$name, $mime 和 $size

$file_path = '...';
$file = fopen($file_path, 'r');

$file_name = basename($file_path);
$file_mime = mime_content_type($file_path);
$file_size = filesize($file_path);

http_download_headers($file_name, $file_mime, $file_size);

fpassthru($file);

响应代理

您还可以使用此软件包轻松地将来自其他地方的响应传递给您的响应,允许您精确代理外部资源。此函数将使用底层流以高效地转发请求

GET

forward_http_get($url);

POST

forward_http_post($url, ['key' => 'value']);

异常处理

如果在HTTP请求过程中发生任何异常,将会抛出一个HttpException,该异常可能是子类型RequestException或ResponseException。RequestException表示您的请求存在问题,如果您请求的URL不存在,可能会得到这种异常。ResponseException表示收到除200以外的状态码,可能是以下类型之一:InformationalResponseExceptionRedirectionResponseExceptionClientErrorResponseExceptionServerErrorResponseException

RequestException对象具有getMethod()getRequestData()getRequestHeaders()方法。ResponseException对象也具有这些方法,并且额外具有getResponseStatusCode()getResponseHeaders()方法。您可以根据需要决定错误捕获的特定程度。

try
{
  $response = http_get($bad_url);
}
catch(HttpException $e)
{
  // if anything goes wrong...
}
try
{
  $response = http_get($url);
}
catch(RequestException $e)
{
  // handle request exception...
}
catch(ResponseException $e)
{
  // handle response exception...
}

注意事项

要使用此包,必须在php.ini中将allow_url_fopen设置为on,默认情况下即为如此。