Rest api with php » History » Version 6

Terence Mill, 2010-11-23 21:03

1 4 Terence Mill
h1. Accessing Redmine beyond API with Redmine.module
2 4 Terence Mill
3 6 Terence Mill
Redmine.module provides a basic API to Redmine, a project tracking system.
4 6 Terence Mill
5 6 Terence Mill
Unfortunately Redmine's REST API is not sufficiently mature (at time of authoring) for the project that needed this module. Progress for this is on the roadmap for Redmine 1.1.0, and this module may be updated to use it.
6 6 Terence Mill
7 6 Terence Mill
Instead of using the REST API, Redmine.module provides access to the Redmine database from Drupal.
8 6 Terence Mill
9 6 Terence Mill
Redmine.module provides a and partially manages a profile.module field for user's Redmine User ID, as well as a Redmine database functions, redmine_query(), and redmine_write_record(). These are wrappers for Drupal's db_query() and drupal_write_record() functions.
10 6 Terence Mill
11 6 Terence Mill
There is also a function for saving (INSERT or UPDATE) Redmine time entries, redmine_time_entry_save().
12 6 Terence Mill
13 5 Terence Mill
"See original article here":http://drupal.org/project/redmine and "projects site":http://drupalmodules.com/module/redmine-api
14 4 Terence Mill
15 1 Jean-Philippe Lang
h1. Using the REST API with PHP
16 1 Jean-Philippe Lang
17 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:
18 1 Jean-Philippe Lang
19 1 Jean-Philippe Lang
<pre>
20 3 Azamat Hackimov
<code class="php">
21 1 Jean-Philippe Lang
<?php
22 1 Jean-Philippe Lang
require_once ('ActiveResource.php');
23 1 Jean-Philippe Lang
24 1 Jean-Philippe Lang
class Issue extends ActiveResource {
25 1 Jean-Philippe Lang
    var $site = 'http://username:password@192.168.199.129:3000/';
26 1 Jean-Philippe Lang
    var $request_format = 'xml'; // REQUIRED!
27 1 Jean-Philippe Lang
}
28 1 Jean-Philippe Lang
29 1 Jean-Philippe Lang
// create a new issue
30 1 Jean-Philippe Lang
$issue = new Issue (array ('subject' => 'XML REST API', 'project_id' => '1'));
31 1 Jean-Philippe Lang
$issue->save ();
32 2 Jean-Philippe Lang
echo $issue->id;
33 1 Jean-Philippe Lang
34 1 Jean-Philippe Lang
// find issues
35 1 Jean-Philippe Lang
$issues = $issue->find ('all');
36 1 Jean-Philippe Lang
for ($i=0; $i < count($issues); $i++) {
37 1 Jean-Philippe Lang
	echo $issues[$i]->subject;
38 1 Jean-Philippe Lang
}
39 1 Jean-Philippe Lang
40 1 Jean-Philippe Lang
// find and update an issue
41 1 Jean-Philippe Lang
$issue->find (1);
42 1 Jean-Philippe Lang
echo $issue->subject;
43 1 Jean-Philippe Lang
$issue->set ('subject', 'This is the new subject')->save ();
44 1 Jean-Philippe Lang
45 1 Jean-Philippe Lang
// delete an issue
46 1 Jean-Philippe Lang
$issue->find (1);
47 1 Jean-Philippe Lang
$issue->destroy ();
48 1 Jean-Philippe Lang
?>
49 3 Azamat Hackimov
</code>
50 1 Jean-Philippe Lang
</pre>