Rest api with php » History » Version 2
Jean-Philippe Lang, 2010-01-17 21:03
slight editing
1 | 1 | Jean-Philippe Lang | h1. Using the REST API with PHP |
---|---|---|---|
2 | |||
3 | 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: |
4 | 1 | Jean-Philippe Lang | |
5 | <pre> |
||
6 | <?php |
||
7 | require_once ('ActiveResource.php'); |
||
8 | |||
9 | class Issue extends ActiveResource { |
||
10 | var $site = 'http://username:password@192.168.199.129:3000/'; |
||
11 | var $request_format = 'xml'; // REQUIRED! |
||
12 | } |
||
13 | |||
14 | // create a new issue |
||
15 | $issue = new Issue (array ('subject' => 'XML REST API', 'project_id' => '1')); |
||
16 | $issue->save (); |
||
17 | 2 | Jean-Philippe Lang | echo $issue->id; |
18 | 1 | Jean-Philippe Lang | |
19 | // find issues |
||
20 | $issues = $issue->find ('all'); |
||
21 | for ($i=0; $i < count($issues); $i++) { |
||
22 | echo $issues[$i]->subject; |
||
23 | } |
||
24 | |||
25 | // find and update an issue |
||
26 | $issue->find (1); |
||
27 | echo $issue->subject; |
||
28 | $issue->set ('subject', 'This is the new subject')->save (); |
||
29 | |||
30 | // delete an issue |
||
31 | $issue->find (1); |
||
32 | $issue->destroy (); |
||
33 | ?> |
||
34 | </pre> |