josantonius / curl
1.1.7
2022-07-18 19:19 UTC
Requires
- php: ^5.6 || ^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.3 || ^2.8
- phpmd/phpmd: ^2.6
- phpunit/phpunit: ^5.7 || ^6.0
- squizlabs/php_codesniffer: ^3.0
README
通过 Curl 库通过 HTTP 协议发送 API 请求。
这是一个使用 cURL 的非常基本的选项。建议使用 Guzzle。
要求
此库支持 PHP 版本 5.6 或更高版本,并兼容 HHVM 版本 3.0 或更高版本。
安装
安装此扩展的首选方式是通过 Composer。
要安装 PHP Curl 库,只需
composer require Josantonius/Curl
前面的命令只会安装必要的文件,如果您希望 下载整个源代码,可以使用
composer require Josantonius/Curl --prefer-source
您也可以使用 Git 克隆整个仓库
git clone https://github.com/Josantonius/PHP-Curl.git
或者 手动安装
wget https://raw.githubusercontent.com/Josantonius/PHP-Curl/master/src/Curl.php
可用方法
此库中的可用方法
- 发送请求并获取响应网站
Curl::request($url, $params, $result);
属性 | 描述 | 类型 | 必需 | 默认 |
---|---|---|---|---|
$url | 获取内容时的 URL。 | string | 是 |
属性 | Key | 描述 | 类型 | 必需 | 默认 |
---|---|---|---|---|---|
$params | 参数。 | array | 否 | array() | |
'referer' | 引用 URL。 | string | 否 | ||
'timeout' | 超时。 | int | 否 | ||
'agent' | Useragent。 | string | 否 | ||
'headers' | HTTP 头。 | array | 否 | ||
'data' | 要发送的参数。 | array | 否 | ||
'type' | 请求类型。 | string | 否 |
属性 | 描述 | 类型 | 必需 | 默认 |
---|---|---|---|---|
$result | 作为数组或对象返回结果。 | string | 否 | 'array' |
返回 (array|object) → response
快速开始
使用 Composer 使用此类
require __DIR__ . '/vendor/autoload.php'; use Josantonius\Curl\Curl;
如果您已手动安装,使用它
require_once __DIR__ . '/Curl.php'; use Josantonius\Curl\Curl;
用法
此库的使用示例
- 发送 GET 请求并作为数组获取响应
Curl::request('https://graph.facebook.com/zuck');
- 发送 GET 请求并作为对象获取响应
Curl::request('https://graph.facebook.com/zuck', false, 'object');
- 发送带参数的 GET 请求并作为数组获取响应
$data = [ 'timeout' => 10, 'referer' => 'http://site.com', ]; Curl::request('https://graph.facebook.com/zuck', $data);
- 发送带参数的 GET 请求并作为对象获取响应
$data = [ 'timeout' => 10, 'referer' => 'http://site.com', ]; Curl::request('https://graph.facebook.com/zuck', $data, 'object');
- 发送 POST 请求并作为数组获取响应
$data = [ 'type' => 'post', 'data' => array('user' => '123456', 'password' => 'xxxxx'), 'timeout' => 10, 'referer' => 'http://' . $_SERVER['HTTP_HOST'], 'headers' => [ 'Content-Type:application/json', 'Authorization:0kdm3hzmb4h3cf', ], ]; Curl::request('https://graph.facebook.com/zuck', $data);
- 发送 POST 请求并获取对象响应
$data = [ 'type' => 'post', 'data' => array('user' => '123456', 'password' => 'xxxxx'), 'timeout' => 10, 'referer' => 'http://' . $_SERVER['HTTP_HOST'], 'headers' => [ 'Content-Type:application/json', 'Authorization:0kdm3hzmb4h3cf', ], ]; Curl::request('https://graph.facebook.com/zuck', $data, 'object');
- 发送 PUT 请求并获取数组响应
$data = [ 'type' => 'put', 'data' => array('email' => 'new@email.com'), 'timeout' => 30, 'referer' => 'http://' . $_SERVER['HTTP_HOST'], 'headers' => [ 'Content-Type:application/json', 'Authorization:0kdm3hzmb4h3cf', ], ]; Curl::request('https://graph.facebook.com/zuck', $data);
- 发送 PUT 请求并获取对象响应
$data = [ 'type' => 'put', 'data' => array('email' => 'new@email.com'), 'timeout' => 30, 'referer' => 'http://' . $_SERVER['HTTP_HOST'], 'headers' => [ 'Content-Type:application/json', 'Authorization:0kdm3hzmb4h3cf', ], ]; Curl::request('https://graph.facebook.com/zuck', $data, 'object');
- 发送 DELETE 请求并获取数组响应
$data = [ 'type' => 'delete', 'data' => ['userId' => 10], 'timeout' => 30, 'referer' => 'http://' . $_SERVER['HTTP_HOST'], 'headers' => [ 'Content-Type:application/json', 'Authorization:0kdm3hzmb4h3cf', ], ]; Curl::request('https://graph.facebook.com/zuck', $data);
- 发送 DELETE 请求并获取对象响应
$data = [ 'type' => 'delete', 'data' => ['userId' => 10], 'timeout' => 30, 'referer' => 'http://' . $_SERVER['HTTP_HOST'], 'headers' => [ 'Content-Type:application/json', 'Authorization:0kdm3hzmb4h3cf', ], ]; Curl::request('https://graph.facebook.com/zuck', $data, 'object');
测试
git clone https://github.com/Josantonius/PHP-Curl.git
cd PHP-Curl
composer install
使用 PHPUnit 运行单元测试
composer phpunit
composer phpcs
运行 PHP Mess Detector 测试以检测代码风格的差异
composer phpmd
运行所有之前的测试
composer tests
赞助
如果这个项目帮助您减少了开发时间,您可以通过 赞助我 来支持我的开源工作 😊
许可
本仓库受 MIT 许可 许可。
版权所有 © 2016-2022, Josantonius