Git Product home page Git Product logo

pest's Introduction

Pest

Pest is a PHP client library for RESTful web services.

Unlike Zend_Rest_Client, which is not really a "REST" client at all (more like RPC-over-HTTP), Pest supports the four REST verbs (GET/POST/PUT/DELETE) and pays attention to HTTP response status codes.

Composer Installation

To install Pest, use the following composer require statement:

{
    "require": {
        "educoder/pest": "1.0.0"
    }
}

Basic Example

Pest's get/post/put/delete() return the raw response body as a string. See the info on PestXML (below) if you're working with XML-based REST services and PestJSON if you're working with JSON.

<?php

$pest = new Pest('http://example.com');

$thing = $pest->get('/things');

$thing = $pest->post('/things', 
	array(
		'name' => "Foo",
		'colour' => "Red"
	)
);

$thing = $pest->put('/things/15',
	array(
		'colour' => "Blue"
	)
);

$pest->delete('/things/15');

?>

Responses with error status codes (4xx and 5xx) raise exceptions.

<?php

try {
	$thing = $pest->get('/things/18');
} catch (Pest_NotFound $e) {
	// 404
	echo "Thing with ID 18 doesn't exist!";
}

try {
	$thing = $pest->post('/things',  array('colour' => "Red"));
} catch (Pest_InvalidRecord $e) {
	// 422
	echo "Data for Thing is invalid because: ".$e->getMessage();
}

?>

PestXML

PestXML is an XML-centric version of Pest, specifically targeted at REST services that return XML data. Rather than returning the raw response body as a string, PestXML will try to parse the service's response into a SimpleXML object.

<?php

$pest = new Pest('http://example.com');

$things = $pest->get('/things.xml');

$colours = $things->xpath('//colour');
foreach($colours as $colour) {
	echo $colour."\n";
}

?>

Similarly, PestJSON is a JSON-centric version of Pest.

Much more detailed examples are available in the examples directory:

TODO

  • Authentication
  • Follow Redirects

License

Copyright (C) 2011 by University of Toronto

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

pest's People

Contributors

zuk avatar djsipe avatar richardkmiller avatar command-tab avatar bjovas avatar mfprimo avatar bartfaitamas avatar gfrenoy avatar jplaza avatar leehblue avatar mayconbordin avatar karpet avatar petertheone avatar torenware avatar themusicman avatar todd1251 avatar warnew avatar pasinter avatar vhpoet avatar

Watchers

James Cloos avatar Kallef Alexandre 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.