Project

General

Profile

Actions

Rest api with php » History » Revision 8

« Previous | Revision 8/14 (diff) | Next »
B Brey, 2011-01-06 21:49


Using the REST API with PHP

Here is an example that uses PHP ActiveResource, a lightweight PHP library that can be used to access Rails' REST APIs:

<?php
require_once ('ActiveResource.php');

class Issue extends ActiveResource {
    var $site = 'http://username:password@192.168.199.129:3000/';
    var $request_format = 'xml'; // REQUIRED!
}

// create a new issue
$issue = new Issue (array ('subject' => 'XML REST API', 'project_id' => '1'));
$issue->save ();
echo $issue->id;

// find issues
$issues = $issue->find ('all');
for ($i=0; $i < count($issues); $i++) {
    echo $issues[$i]->subject;
}

// find and update an issue
$issue->find (1);
echo $issue->subject;
$issue->set ('subject', 'This is the new subject')->save ();

// delete an issue
$issue->find (1);
$issue->destroy ();
?>

Known issues

If you are working with large descriptions the webserver can return a 417 error (Bad Expectation).
You should replace line 381 in ActiveResource.php with the following code:

curl_setopt ($ch, CURLOPT_HTTPHEADER, array ('Expect:',"Content-Type: text/xml", "Length: " . strlen ($params)));

Updated by B Brey almost 14 years ago · 8 revisions