Giter Club home page Giter Club logo

phpactiveresource's Introduction

PHP ActiveResource

Click here to lend your support to: phpactiveresource and make a donation at www.pledgie.com !

This is a basic PHP class for accessing Ruby on Rails REST APIs in an ActiveResource style of coding. The benefit is easier use of RoR-based REST services without having to roll your own CURL-based client each time. Hopefully this class saves a few people some time coding in PHP against RoR-based REST services. It’s by no means an exhaustive port, and some methods are missing, but it does try to cover all the basics.

Note: You will need the php curl extension installed on your system. On Ubuntu, you can install them with:

sudo apt-get install php5-curl

Usage

<?php

require_once ('ActiveResource.php');

class Song extends ActiveResource {
    var $site = 'http://localhost:3000/';
    var $element_name = 'songs';
}

// create a new item
$song = new Song (array ('artist' => 'Joe Cocker', 'title' => 'A Little Help From My Friends'));
$song->save ();

// fetch and update an item, chaining statements
$song->find (44)->set ('title', 'The River')->save ();

// fetch and update, line by line
$song->find (44);
$song->title = 'The River';
$song->save ();

// get all songs
$songs = $song->find ('all');

// delete a song
$song->find (44);
$song->destroy ();

// custom method
$songs = $song->get ('by_year', array ('year' => 1999));

?>

Extra URL params

If you want to add extra params to the end of the url eg: an API key, you can set $extra_params

<?php

require_once ('ActiveResource.php');

class Song extends ActiveResource {
    var $site = 'http://localhost:3000/';
    var $element_name = 'songs';
    var $extra_params = '?key=123456789';
}

Extra Http Headers

If you need to add extra http request headers you can set $request_headers

<?php

require_once ('ActiveResource.php');

class Song extends ActiveResource {
    var $site = 'http://localhost:3000/';
    var $element_name = 'songs';
    var $request_headers = array("x-api-key: some-api-key-here");
}

phpactiveresource's People

Contributors

bmaynard avatar jbroadway avatar jhirbour avatar lux avatar rafaelp avatar sheldonrampton avatar

Watchers

 avatar  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.