Rest api with php » History » Version 1
Jean-Philippe Lang, 2010-01-17 20:53
| 1 | 1 | Jean-Philippe Lang | h1. Using the REST API with PHP |
|---|---|---|---|
| 2 | |||
| 3 | Here is an example that uses "phpactiveresource":http://wiki.github.com/lux/phpactiveresource/, a lightweight PHP library that can be used to access Rails' REST APIs: |
||
| 4 | |||
| 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 | |||
| 18 | // find issues |
||
| 19 | $issues = $issue->find ('all'); |
||
| 20 | for ($i=0; $i < count($issues); $i++) { |
||
| 21 | echo $issues[$i]->subject; |
||
| 22 | } |
||
| 23 | |||
| 24 | // find and update an issue |
||
| 25 | $issue->find (1); |
||
| 26 | echo $issue->subject; |
||
| 27 | $issue->set ('subject', 'This is the new subject')->save (); |
||
| 28 | |||
| 29 | // delete an issue |
||
| 30 | $issue->find (1); |
||
| 31 | $issue->destroy (); |
||
| 32 | ?> |
||
| 33 | </pre> |