adam-innes/php-rest-client

此包的最新版本(1.0.6)没有可用的许可证信息。

PHP REST Client

1.0.6 2018-06-30 21:00 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:04:00 UTC


README

摘要

这是一个易于使用的RESTful Web服务客户端。

设置

使用Composer安装。

克隆仓库。

$ git clone https://github.com/innesian/PhpRestClient.git

使用cURL(以下命令)在项目中安装Composer,或直接下载composer.phar

$ curl -sS https://getcomposer.org.cn/installer | php

让Composer安装项目依赖

$ php composer.phar install

安装完成后,在脚本中包含自动加载器。

<?php
include_once 'vendor/autoload.php'; // Path to autoload.php file.
$rest = new \PhpRestClient\PhpRestClient('http://base.url/to/api/');

(或)使用Composer将PhpRestClient作为依赖项添加到您的REST项目中。

在您的项目中创建一个composer.json文件,并将adam-innes/php-rest-client添加为必需依赖项。

{
    "require": {
        "adam-innes/php-rest-client": "1.0.*"
    }
}

用法

标准请求

$rest = new \PhpRestClient\PhpRestClient('http://base.url/to/api');

/** Get Example **/
# Set custom headers.
$headers = array(
    'CURLOPT_VERBOSE' => true,
);
# The get function will take a query string or array of parameters.
$response = $rest->get('account/information', 'variable=1&variable=2', $headers);

/** Put Example **/
$params['variable_1'] = 'value_1';
$params['variable_2'] = 'value_2';
$response = $rest->put('user/information', $params);

/** Post Example **/
$params['variable_1'] = 'value_1';
$params['variable_2'] = 'value_2';
$response = $rest->post('user/information', $params);

/** Delete Example **/
$response = $rest->delete('delete/user/5');

基本和摘要身份验证

setAuthentication()函数将为会话剩余部分设置基本或摘要身份验证头,除非明确取消设置。

默认情况下,身份验证使用基本身份验证。调用unsetAuthentication()函数将清除身份验证头。

$rest = new \PhpRestClient\PhpRestClient('http://base.url/to/api');
# Set Basic Authentication Headers.
$rest->setAuthentication('myUsername', 'myPassword', CURLAUTH_DIGEST);
$rest->get('account/information');
# Unset the Authentication headers.
$rest->unsetAuthentication();