Rest api with php » History » Version 8
B Brey, 2011-01-06 21:49
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 | 3 | Azamat Hackimov | <code class="php"> |
7 | 1 | Jean-Philippe Lang | <?php |
8 | require_once ('ActiveResource.php'); |
||
9 | |||
10 | class Issue extends ActiveResource { |
||
11 | var $site = 'http://username:password@192.168.199.129:3000/'; |
||
12 | var $request_format = 'xml'; // REQUIRED! |
||
13 | } |
||
14 | |||
15 | // create a new issue |
||
16 | $issue = new Issue (array ('subject' => 'XML REST API', 'project_id' => '1')); |
||
17 | $issue->save (); |
||
18 | 2 | Jean-Philippe Lang | echo $issue->id; |
19 | 1 | Jean-Philippe Lang | |
20 | // find issues |
||
21 | $issues = $issue->find ('all'); |
||
22 | for ($i=0; $i < count($issues); $i++) { |
||
23 | echo $issues[$i]->subject; |
||
24 | } |
||
25 | |||
26 | // find and update an issue |
||
27 | $issue->find (1); |
||
28 | echo $issue->subject; |
||
29 | $issue->set ('subject', 'This is the new subject')->save (); |
||
30 | |||
31 | // delete an issue |
||
32 | $issue->find (1); |
||
33 | $issue->destroy (); |
||
34 | ?> |
||
35 | 3 | Azamat Hackimov | </code> |
36 | 1 | Jean-Philippe Lang | </pre> |
37 | 8 | B Brey | |
38 | *Known issues* |
||
39 | |||
40 | If you are working with large descriptions the webserver can return a 417 error (Bad Expectation). |
||
41 | You should replace line 381 in ActiveResource.php with the following code: |
||
42 | |||
43 | <pre>curl_setopt ($ch, CURLOPT_HTTPHEADER, array ('Expect:',"Content-Type: text/xml", "Length: " . strlen ($params)));</pre> |