Rest api with php » History » Version 5

Terence Mill, 2010-11-23 21:03

1 4 Terence Mill
h1. Accessing Redmine beyond API with Redmine.module
2 4 Terence Mill
3 5 Terence Mill
"See original article here":http://drupal.org/project/redmine and "projects site":http://drupalmodules.com/module/redmine-api
4 4 Terence Mill
5 1 Jean-Philippe Lang
h1. Using the REST API with PHP
6 1 Jean-Philippe Lang
7 2 Jean-Philippe Lang
Here is an example that uses "PHP ActiveResource":http://wiki.github.com/lux/phpactiveresource/, a lightweight PHP library that can be used to access Rails' REST APIs:
8 1 Jean-Philippe Lang
9 1 Jean-Philippe Lang
<pre>
10 3 Azamat Hackimov
<code class="php">
11 1 Jean-Philippe Lang
<?php
12 1 Jean-Philippe Lang
require_once ('ActiveResource.php');
13 1 Jean-Philippe Lang
14 1 Jean-Philippe Lang
class Issue extends ActiveResource {
15 1 Jean-Philippe Lang
    var $site = 'http://username:password@192.168.199.129:3000/';
16 1 Jean-Philippe Lang
    var $request_format = 'xml'; // REQUIRED!
17 1 Jean-Philippe Lang
}
18 1 Jean-Philippe Lang
19 1 Jean-Philippe Lang
// create a new issue
20 1 Jean-Philippe Lang
$issue = new Issue (array ('subject' => 'XML REST API', 'project_id' => '1'));
21 1 Jean-Philippe Lang
$issue->save ();
22 2 Jean-Philippe Lang
echo $issue->id;
23 1 Jean-Philippe Lang
24 1 Jean-Philippe Lang
// find issues
25 1 Jean-Philippe Lang
$issues = $issue->find ('all');
26 1 Jean-Philippe Lang
for ($i=0; $i < count($issues); $i++) {
27 1 Jean-Philippe Lang
	echo $issues[$i]->subject;
28 1 Jean-Philippe Lang
}
29 1 Jean-Philippe Lang
30 1 Jean-Philippe Lang
// find and update an issue
31 1 Jean-Philippe Lang
$issue->find (1);
32 1 Jean-Philippe Lang
echo $issue->subject;
33 1 Jean-Philippe Lang
$issue->set ('subject', 'This is the new subject')->save ();
34 1 Jean-Philippe Lang
35 1 Jean-Philippe Lang
// delete an issue
36 1 Jean-Philippe Lang
$issue->find (1);
37 1 Jean-Philippe Lang
$issue->destroy ();
38 1 Jean-Philippe Lang
?>
39 3 Azamat Hackimov
</code>
40 1 Jean-Philippe Lang
</pre>