此包已被弃用,不再维护。作者建议使用 guzzlehttp/guzzle 包。

通过 Curl 库通过 HTTP 协议发送 API 请求。

1.1.7 2022-07-18 19:19 UTC

This package is auto-updated.

Last update: 2022-07-18 19:29:10 UTC


README

Latest Stable Version License

西班牙语版本

通过 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

或者 手动安装

下载 Curl.php:

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');

测试

要运行 测试,您只需要 composer 并执行以下命令

git clone https://github.com/Josantonius/PHP-Curl.git

cd PHP-Curl

composer install

使用 PHPUnit 运行单元测试

composer phpunit

使用 PSR2 代码标准测试使用 PHPCS

composer phpcs

运行 PHP Mess Detector 测试以检测代码风格的差异

composer phpmd

运行所有之前的测试

composer tests

赞助

如果这个项目帮助您减少了开发时间,您可以通过 赞助我 来支持我的开源工作 😊

许可

本仓库受 MIT 许可 许可。

版权所有 © 2016-2022, Josantonius