Rest api with php » History » Version 14
Kevin Saliou, 2013-06-20 11:28
| 1 | 1 | Jean-Philippe Lang | h1. Using the REST API with PHP |
|---|---|---|---|
| 2 | |||
| 3 | 14 | Kevin Saliou | h2. php-redmine-api Project |
| 4 | |||
| 5 | * A simple Object Oriented PHP Redmine API client : |
||
| 6 | ** https://github.com/kbsali/php-redmine-api |
||
| 7 | |||
| 8 | h2. The PHP ActiveResource way |
||
| 9 | |||
| 10 | 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: |
| 11 | 1 | Jean-Philippe Lang | |
| 12 | <pre> |
||
| 13 | 3 | Azamat Hackimov | <code class="php"> |
| 14 | 1 | Jean-Philippe Lang | <?php |
| 15 | require_once ('ActiveResource.php'); |
||
| 16 | |||
| 17 | class Issue extends ActiveResource { |
||
| 18 | var $site = 'http://username:password@192.168.199.129:3000/'; |
||
| 19 | var $request_format = 'xml'; // REQUIRED! |
||
| 20 | } |
||
| 21 | |||
| 22 | // create a new issue |
||
| 23 | $issue = new Issue (array ('subject' => 'XML REST API', 'project_id' => '1')); |
||
| 24 | $issue->save (); |
||
| 25 | 2 | Jean-Philippe Lang | echo $issue->id; |
| 26 | 1 | Jean-Philippe Lang | |
| 27 | // find issues |
||
| 28 | $issues = $issue->find ('all'); |
||
| 29 | for ($i=0; $i < count($issues); $i++) { |
||
| 30 | echo $issues[$i]->subject; |
||
| 31 | } |
||
| 32 | |||
| 33 | // find and update an issue |
||
| 34 | $issue->find (1); |
||
| 35 | echo $issue->subject; |
||
| 36 | $issue->set ('subject', 'This is the new subject')->save (); |
||
| 37 | 12 | Lucas Bickel | // update status |
| 38 | $issue->set ('status_id', 2)->save(); |
||
| 39 | 1 | Jean-Philippe Lang | |
| 40 | // delete an issue |
||
| 41 | $issue->find (1); |
||
| 42 | $issue->destroy (); |
||
| 43 | ?> |
||
| 44 | 3 | Azamat Hackimov | </code> |
| 45 | 1 | Jean-Philippe Lang | </pre> |
| 46 | 8 | B Brey | |
| 47 | *Known issues* |
||
| 48 | |||
| 49 | 10 | Mischa The Evil | * If you are working with large descriptions, the webserver can return a 417 error (Bad Expectation). |
| 50 | You should replace line 381 in _ActiveResource.php_ with the following code: |
||
| 51 | <pre><code class="php">curl_setopt ($ch, CURLOPT_HTTPHEADER, array ('Expect:',"Content-Type: text/xml", "Length: " . strlen ($params)));</code></pre> |
||
| 52 | 11 | Robert Nürnberg | * If you are trying to use the class to write @time_entries@, you will get a 404 response (#9375). The reason for that is that the class is unable to create the correct singular form from the pluralized version of the entity name. (edit 2011-10-06: according to the website the bug was fixed in the latest release: https://github.com/lux/phpactiveresource/issues/7#issuecomment-2300502) |
| 53 | 10 | Mischa The Evil | This can be patched in _ActiveResource.php_ by: |
| 54 | # adding the class variable @$sOriginalElementName@: |
||
| 55 | <pre><code class="php">protected $sOriginalElementName = '';</code></pre> |
||
| 56 | # changing the constructor to set the class variable using the original entity name before the pluralization is called: |
||
| 57 | <pre><code class="php"> |
||
| 58 | 9 | Robert Nürnberg | function __construct ($data = array ()) { |
| 59 | $this->_data = $data; |
||
| 60 | 1 | Jean-Philippe Lang | |
| 61 | 9 | Robert Nürnberg | // add this line here - to store the original name of the entity |
| 62 | $this->sOriginalElementName = ($this->element_name ? $this->element_name : strtolower (get_class ($this))); |
||
| 63 | // Allow class-defined element name or use class name if not defined |
||
| 64 | $this->element_name = ($this->element_name ? $this->pleuralize ($this->element_name) : $this->pleuralize (strtolower (get_class ($this)))); |
||
| 65 | ... |
||
| 66 | 10 | Mischa The Evil | </code></pre> |
| 67 | # and then changing the method @_send_and_receive@ to use @$sOriginalElementName@ instead of @substr ($this->element_name, 0, -1)@: |
||
| 68 | 9 | Robert Nürnberg | <pre><code class="php"> |
| 69 | 10 | Mischa The Evil | function _send_and_receive ($url, $method, $data = array ()) { |
| 70 | 13 | Kevin Saliou | $params = ''; |
| 71 | $el = $this->sOriginalElementName;//substr ($this->element_name, 0, -1); |
||
| 72 | if ($this->request_format == 'url') { |
||
| 73 | ... |
||
| 74 | </code></pre> |