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 1 Jean-Philippe Lang
3 14 Kevin Saliou
h2. php-redmine-api Project
4 14 Kevin Saliou
5 14 Kevin Saliou
* A simple Object Oriented PHP Redmine API client :
6 14 Kevin Saliou
** https://github.com/kbsali/php-redmine-api
7 14 Kevin Saliou
8 14 Kevin Saliou
h2. The PHP ActiveResource way
9 14 Kevin Saliou
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 1 Jean-Philippe Lang
<pre>
13 3 Azamat Hackimov
<code class="php">
14 1 Jean-Philippe Lang
<?php
15 1 Jean-Philippe Lang
require_once ('ActiveResource.php');
16 1 Jean-Philippe Lang
17 1 Jean-Philippe Lang
class Issue extends ActiveResource {
18 1 Jean-Philippe Lang
    var $site = 'http://username:password@192.168.199.129:3000/';
19 1 Jean-Philippe Lang
    var $request_format = 'xml'; // REQUIRED!
20 1 Jean-Philippe Lang
}
21 1 Jean-Philippe Lang
22 1 Jean-Philippe Lang
// create a new issue
23 1 Jean-Philippe Lang
$issue = new Issue (array ('subject' => 'XML REST API', 'project_id' => '1'));
24 1 Jean-Philippe Lang
$issue->save ();
25 2 Jean-Philippe Lang
echo $issue->id;
26 1 Jean-Philippe Lang
27 1 Jean-Philippe Lang
// find issues
28 1 Jean-Philippe Lang
$issues = $issue->find ('all');
29 1 Jean-Philippe Lang
for ($i=0; $i < count($issues); $i++) {
30 1 Jean-Philippe Lang
	echo $issues[$i]->subject;
31 1 Jean-Philippe Lang
}
32 1 Jean-Philippe Lang
33 1 Jean-Philippe Lang
// find and update an issue
34 1 Jean-Philippe Lang
$issue->find (1);
35 1 Jean-Philippe Lang
echo $issue->subject;
36 1 Jean-Philippe Lang
$issue->set ('subject', 'This is the new subject')->save ();
37 12 Lucas Bickel
// update status
38 12 Lucas Bickel
$issue->set ('status_id', 2)->save();
39 1 Jean-Philippe Lang
40 1 Jean-Philippe Lang
// delete an issue
41 1 Jean-Philippe Lang
$issue->find (1);
42 1 Jean-Philippe Lang
$issue->destroy ();
43 1 Jean-Philippe Lang
?>
44 3 Azamat Hackimov
</code>
45 1 Jean-Philippe Lang
</pre>
46 8 B Brey
47 8 B Brey
*Known issues*
48 8 B Brey
49 10 Mischa The Evil
* If you are working with large descriptions, the webserver can return a 417 error (Bad Expectation).
50 10 Mischa The Evil
You should replace line 381 in _ActiveResource.php_ with the following code:
51 10 Mischa The Evil
<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 10 Mischa The Evil
# adding the class variable @$sOriginalElementName@:
55 10 Mischa The Evil
<pre><code class="php">protected $sOriginalElementName = '';</code></pre>
56 10 Mischa The Evil
# changing the constructor to set the class variable using the original entity name before the pluralization is called:
57 10 Mischa The Evil
<pre><code class="php">
58 9 Robert Nürnberg
function __construct ($data = array ()) {
59 9 Robert Nürnberg
  $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 9 Robert Nürnberg
  $this->sOriginalElementName = ($this->element_name ? $this->element_name : strtolower (get_class ($this)));
63 9 Robert Nürnberg
  // Allow class-defined element name or use class name if not defined
64 9 Robert Nürnberg
  $this->element_name = ($this->element_name ? $this->pleuralize ($this->element_name) : $this->pleuralize (strtolower (get_class ($this))));
65 9 Robert Nürnberg
...
66 10 Mischa The Evil
</code></pre>
67 10 Mischa The Evil
# 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 13 Kevin Saliou
  $el = $this->sOriginalElementName;//substr ($this->element_name, 0, -1);
72 13 Kevin Saliou
  if ($this->request_format == 'url') {
73 13 Kevin Saliou
...
74 13 Kevin Saliou
</code></pre>