Rest api with php » History » Version 9

Robert Nürnberg, 2011-10-05 12:31

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