vivaserver/restful_agent 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

vivaserver/restful_agent

Composer 安装命令:

composer require vivaserver/restful_agent

包简介

A little library for accessing URLs using Curl in a RESTful way

README 文档

README

This is a simple Composer package for accessing RESTful resources using Curl. Enjoy!

Installation

The easiest way is to install it as any Composer package. Just add an entry to your composer.json file requiring the latest available version:

...
"require": {
  "vivaserver/restful_agent": "dev-master"
  ...
}
...

The package will be automatically installed when you execute the composer install command.

Usage

Create a new instance of the RESTful Agent after requiring the Composer autoloader and you should be ready to go.

require 'vendor/autoload.php';
$agent = new Resftful\Agent;

Later you should be able to use the $agent instance to perform any HTTP request.

DELETE request

$agent->delete('http://www.example.com/resources/37');

GET request

$agent->get('http://www.example.com/resources');

POST request

Pass POST parameters as an associative array after the URL.

$agent->post('http://www.example.com/resources',array('id'=>$id,'name'=>$name,'description'=>$description));

PUT request

Pass PUT parameters as an associative array after the URL.

$agent->put('http://www.example.com/resources/25',array('description'=>$description));

Return values

The library expects that the RESTful resource will use HTTP response codes to acknowledge the status of it's returned message. Thus, the return values of the library methods is always an object with two properties:

  • code

    The HTTP status code of the resource response.

  • body

    The proper body of the returned response.

Handling return values

Knowing the above, just take into consideration the response code to act on the correct status of the resource. For example:

$response = $agent->get('http://www.example.com/resources/54');
$result = $response->body;
switch ($response->code) {
  case 200:
    return $result;
  break;

  case 404:
    return NULL;
  break;

  case 500:
    error_log($result);
  break;
}

Notes

As expected, the return value of all the methods is the output of the resource to the request.

But please not that, on library failure, an Exception will be thrown. So it's advisable to use it inside a try/catch block, like so:

try {
  $agent->delete('http://www.example.com/resource/37');
}
catch (Exception $e) {
  error_log($e->getMessage());
}

License

This software is released under the MIT License.

Copyright

©2013 Cristian R. Arroyo

统计信息

  • 总下载量: 17
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-07-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固