segpacto/yii2-httpfull

将 'Httpful' 扩展转换为 yii2 扩展

安装次数: 15,986

依赖项: 0

建议者: 0

安全: 0

星标: 1

关注者: 2

分支: 0

开放性问题: 0

类型:yii2-extension

dev-master 2016-08-22 15:50 UTC

This package is not auto-updated.

Last update: 2024-09-18 18:20:17 UTC


README

将 'Httpful' 扩展转换为 yii2 扩展

安装

安装此扩展的首选方法是使用 composer

由于它未在 packages.org 上发布,需要手动使用 composer 添加到您的 composer.json 中

"repositories":[
    {
        "type": "git",
        "url": "https://path.to/your/repo"
    }
]

since the file is local the 'url' should be "url" : "file:///path_to_your_file"

运行以下命令之一

php composer.phar require --prefer-dist segpacto/yii2-httpfull "*"

或添加

"segpacto/yii2-httpfull": "*"

到您的 composer.json 文件的 require 部分。

使用方法

扩展安装后,只需在代码中通过以下方式使用它

// Make a request to the GitHub API with a custom
// header of "X-Trvial-Header: Just as a demo".
$url = "https://api.github.com/users/nategood";
$response = \HttpFull\Request::get($url)
    ->expectsJson()
    ->withXTrivialHeader('Just as a demo')
    ->send();

echo "{$response->body->name} joined GitHub on " .
                        date('M jS', strtotime($response->body->created_at)) ."\n";

示例 1:发送一个 GET 请求。自动解析 JSON 响应。库会注意到响应中的 JSON Content-Type 并自动将响应解析为原生 PHP 对象。

$uri = "https://www.googleapis.com/freebase/v1/mqlread?query=%7B%22type%22:%22/music/artist%22%2C%22name%22:%22The%20Dead%20Weather%22%2C%22album%22:%5B%5D%7D";
$response = \HttpFull\Request::get($uri)->send();

echo 'The Dead Weather has ' . count($response->body->result->album) . " albums.\n";

示例 2

$uri = 'https://example.net/person.xml';
$response = \HttpFull\Request::get($uri)
    ->expectsXml()
    ->send();

echo "Name: $response->body->name";

注意:这基于由 nategood 共享的 package。这只是一个转换为作为 Yii2 扩展使用的转换。有关该包的进一步说明和文档,请直接访问他们的 文档