Skip to content

Instantly share code, notes, and snippets.

@heavenshell
Created December 17, 2010 15:08
Show Gist options
  • Save heavenshell/745074 to your computer and use it in GitHub Desktop.
Save heavenshell/745074 to your computer and use it in GitHub Desktop.
RESTful Client library
<?php
/**
* Kyoto Tycoon's PHP RESTful client example.
*
* PHP version 5.2
*
* Copyright (c) 2010 Shinya Ohyanagi, All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Shinya Ohyanagi nor the names of his
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Net
* @package Net_KyotoTycoon
* @version $id$
* @copyright (c) 2010 Shinya Ohyanagi
* @author Shinya Ohyanagi <sohyanagi@gmail.com>
* @license New BSD License
*/
/**
* @see HTTP_Request2
*/
require_once 'HTTP/Request2.php';
/**
* Net_KyotoTycoon_Rest
*
* @category Net
* @package Net_KyotoTycoon
* @version $id$
* @copyright (c) 2010 Shinya Ohyanagi
* @author Shinya Ohyanagi <sohyanagi@gmail.com>
* @license New BSD License
*/
class Net_KyotoTycoon_Rest
{
/**
* Version.
*/
const VERSION = '0.0.1';
/**
* Http Client.
*
* @var mixed
* @access private
*/
private $_client = null;
/**
* Base uri.
*
* @var mixed
* @access private
*/
private $_base = null;
/**
* Constructor
*
* @param array $args Http client options.
* @access public
* @return void
*/
public function __construct(array $args = array())
{
$host = isset($args['host']) ? $args['host'] : '127.0.0.1';
$port = isset($args['port']) ? $args['port'] : 1978;
$timeout = isset($args['timeout']) ? $args['timeout'] : 30;
$agent = __CLASS__ . '/' . self::VERSION;
$adapter = 'HTTP_Request2_Adapter_Socket';
$config = array('adapter' => $adapter, 'timeout' => $timeout);
$client = new HTTP_Request2(null, null, $config);
$client->setHeader('user-agent', $agent);
$this->_client = $client;
$this->_base = "http://$host:$port/";
}
/**
* Put request.
*
* @param mixed $key Key
* @param mixed $value Value
* @param mixed $xt Expire time
* @access public
* @return bool True:Success to put, False:Fail to put.
*/
public function set($key, $value, $xt = null)
{
$uri = $this->_base . urlencode($key);
$headers = array(
'Content-Length' => strlen($value),
'Connection' => 'Keep-Alive',
'Keep-Alive' => 300,
);
if ($xt !== null) {
$xt = time() + $xt;
$headers['X-Kt-Xt'] = $xt;
}
$response = $this->_client->setMethod(HTTP_Request2::METHOD_PUT)
->setHeader($headers)
->setUrl($uri)
->setBody($value)
->send();
return $response->getStatus() === 201;
}
/**
* Delete request.
*
* @param mixed $key Key
* @access public
* @return bool True:Success to delete, False:Fail to delete.
*/
public function remove($key)
{
$uri = $this->_base . urlencode($key);
$headers = array(
'Content-Length' => 0,
'Connection' => 'Keep-Alive',
'Keep-Alive' => 300,
);
$response = $this->_client->setMethod(HTTP_Request2::METHOD_DELETE)
->setHeader($headers)
->setUrl($uri)
->setBody('')
->send();
return $response->getStatus() === 204;
}
/**
* Get request.
*
* @param mixed $key Key
* @access public
* @return mixed
*/
public function get($key)
{
$uri = $this->_base . urlencode($key);
$headers = array(
'Content-Length' => 0,
'Connection' => 'Keep-Alive',
'Keep-Alive' => 300,
);
$response = $this->_client->setMethod(HTTP_Request2::METHOD_GET)
->setHeader($headers)
->setUrl($uri)
->setBody('')
->send();
if ($response->getStatus() !== 200) {
return null;
}
return $response->getBody();
}
}
<?php
require_once 'Net/KyotoTycoon/Rest.php';
$args = array('port' => 1979);
$client = new Net_KyotoTycoon_Rest($args);
assert($client->set('foo', 'bar'));
assert($client->get('foo') === 'bar'); // bar
assert($client->remove('foo'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment