m6web/restconnection

该包已被弃用且不再维护。未建议替代包。
此包最新版本(1.0.0)的许可信息不可用。

PHP类,用于轻松地向REST API发送请求。

1.0.0 2019-03-04 14:44 UTC

This package is not auto-updated.

Last update: 2020-01-21 17:52:18 UTC


README

此仓库以“原样”形式发布自已删除的原始版本的1.0.0 tarball,位于https://github.com/oziks/RESTConnection

您可以通过要求m6web/restconnection来使用此包,然后您的任何依赖项都应该使用它,因为我们正在替换原始版本。

以下为原始README内容

RESTConnection是一个PHP类,用于轻松地向REST API发送请求

关于

过程如下

  1. 实例化一个Client对象,传递连接到您选择的REST API的通用参数。例如:服务的base url、凭证、头部(例如指定发送和接受的内容类型)

  2. 使用“request()”方法向此API发送请求,使用参数:url路径、(可选)要发送的数据以及要使用的方法(动词,例如:get、post、put...)。

  3. 获取结果http状态码、主体、头部、错误信息

尽管RESTConnection支持大多数使用的HTTP动词(get、post、put、delete、patch),您可能处于只支持get/post的情况(您的防火墙可能阻止其他动词)。在这种情况下,您可以设置兼容性模式为true。然后,每个delete、put、patch动词都将作为POST传递,并添加一个特殊的头部X-HTTP-Method-Override。另一方面,您也可能想针对特定请求强制执行此操作。想想谷歌翻译,通常将待翻译的单词作为GET。如果您想翻译整个段落,您必须将其作为POST传递并强制X-HTTP-Method-Override为GET。您可以通过只需向请求参数列表中添加一个覆盖动词来轻松完成此操作。

示例

获取Twitter公开推文

// Initialize the header of our future requests, specifying the format we want to use in request and response (json)
$requestHeader = array('Accept: application/json', 'Content-Type: application/json');
// Create the Client object, for now, no credential needed as we get only public tweets
$testAPI = new RESTConnection\Client('https://api.twitter.com/1/', $requestHeader);

// Issue a GET request on 'https://api.twitter.com/1/statuses/public_timeline.json'
if($testAPI->request('statuses/public_timeline.json'))
{
  // Display the tweets
  var_dump(json_decode($testAPI->getResponseBody(), true));
}
else
{
  // Something went wrong
  var_dump($testAPI->getLastError());
}

向campfire发送消息,突出显示它,然后更改房间主题

// Initialize the header of our future requests, specifying the format we want to use in request and response (json)
$requestHeader = array('Accept: application/json', 'Content-Type: application/json');
// Create the Client object, this time, credential are specified
$testAPI = new RESTConnection\Client('https://your.campfirenow.com/', $requestHeader, 'your_token_here', 'X');

// Issue a POST request on 'https://your.campfirenow.com/room/your_room_id/speak.json'
if($testAPI->request('room/your_room_id/speak.json', json_encode(array('message' => array('body' => "Hello"))), RESTConnection\Client::POST)))
{
  // lastStatusCode should be 201
  var_dump($testAPI->getLastStatusCode());
  // Response body contains the message id
  $result = (json_decode($testAPI->getResponseBody(), true));

  // star the message
  $messageid = $result['message']['id'];
  $testAPI->request(sprintf('messages/%s/star.json', $messageid), array(), RESTConnection\Client::POST);

  // unstar it
  // $testAPI->request(sprintf('messages/%s/star.json', $messageid), array(), RESTConnection\Client::DELETE);
}
else
{
  // Something went wrong
  var_dump($testAPI->getLastError());
}

// Issue a PUT request on 'https://your.campfirenow.com/room/your_room_id.json'
$testAPI->request('room/your_room_id.json', json_encode(array('room' => array('topic' => "this room is not about cats"))), RESTConnection\Client::PUT);

致谢

此代码部分基于此文章:http://www.gen-x-design.com/archives/making-restful-requests-in-php/。感谢其作者。