Git Product home page Git Product logo

hyperclient's Introduction

HyperClient

A simple CURL based REST client for PHP with local cache support.

UPDATE: Just learned there is a ruby client of the same name. I need to come up with something different. Any ideas?

For this client to work, your API must use Web Linking headers. It comes with an example against the FoxyCart Hypermedia API sandbox. For more information on the API shown here, see the FoxyCart documentation.

We'd love to know what you think about it. Feel free to ping Luke Stokes with any questions or comments.

##Install

Along with PHP and curl, the client requires a caching mechanism to store responses with ETags. It currently includes a basic APC example as well as a file based example. To use the file based cache, you must have a private folder (not readable by the web or others on your server) that you can write to and read from. See the comments for more information.

Files

  • HyperClient.php: The generic client. Currently has a couple example caching implementations in it as well. Requires curl. This is the only file you need if you're looking for a Hypermedia API client.
  • ExampleDisplay.php: A class used for displaying the output from the client.
  • example.php: This is the file you actually load up in your browser to see the example output.
  • Array2XML.php: used by ExampleDisplay.php to convert arrays to XML when using application/xml as a request content type.

Running

Once you have things configured, simply fire up example.php to see the requests and responses. Feel free to fork it and play with your own examples. The basic commands are as follows:

$resp = $client->get($uri,$data,$headers);
$resp = $client->post($uri,$data,$headers);
$resp = $client->put($uri,$data,$headers);
$resp = $client->patch($uri,$data,$headers);
$resp = $client->delete($uri,$data,$headers);

It comes with some nice hypermedia-friendly methods like $client->getLink($rel) and $client->getLocation(). It also formats the body of the response into either a SimpleXMLElement or json object. See the inline code comments for details.

<?php
require __DIR__."/HyperClient.php";

// setup APC cache
$cache = new CacheAPC();
// or File based cache
// $cache = new CacheFile('/path/to/my/cache/folder');
// or add your own... (it's really easy)

// if your API has a proper uri for its link relationships...
$rel_base_uri = 'https://api.foxycart.com/rels';

// setup client
$client = new HyperClient($cache,$rel_base_uri);

// do stuff!
$resp = $client->get('https://api-sandbox.foxycart.com',null,array('FOXYCART-API-VERSION' => 1));

print '<pre>' . htmlspecialchars(print_r($resp,true)) . '</pre>';

Props

This was heavily inspired and influenced by Ed Finkler's Resty library. If you enjoy it, please give him a shout out.

hyperclient's People

Contributors

lukestokes avatar phred avatar jasonrhodes avatar

Watchers

James Cloos avatar Eric Smith avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.