Project

General

Profile

Rest api with php » History » Version 12

Lucas Bickel, 2012-06-20 01:41
it took me ages to grok updating status in PHP, in the end i had a facepalm moment that i would like to spare others from

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 12 Lucas Bickel
// update status
31
$issue->set ('status_id', 2)->save();
32 1 Jean-Philippe Lang
33
// delete an issue
34
$issue->find (1);
35
$issue->destroy ();
36
?>
37 3 Azamat Hackimov
</code>
38 1 Jean-Philippe Lang
</pre>
39 8 B Brey
40
*Known issues*
41
42 10 Mischa The Evil
* If you are working with large descriptions, the webserver can return a 417 error (Bad Expectation).
43
You should replace line 381 in _ActiveResource.php_ with the following code:
44
<pre><code class="php">curl_setopt ($ch, CURLOPT_HTTPHEADER, array ('Expect:',"Content-Type: text/xml", "Length: " . strlen ($params)));</code></pre>
45 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)
46 10 Mischa The Evil
This can be patched in _ActiveResource.php_ by:
47
# adding the class variable @$sOriginalElementName@:
48
<pre><code class="php">protected $sOriginalElementName = '';</code></pre>
49
# changing the constructor to set the class variable using the original entity name before the pluralization is called:
50
<pre><code class="php">
51 9 Robert Nürnberg
function __construct ($data = array ()) {
52
  $this->_data = $data;
53 1 Jean-Philippe Lang
54 9 Robert Nürnberg
  // add this line here - to store the original name of the entity
55
  $this->sOriginalElementName = ($this->element_name ? $this->element_name : strtolower (get_class ($this)));
56
  // Allow class-defined element name or use class name if not defined
57
  $this->element_name = ($this->element_name ? $this->pleuralize ($this->element_name) : $this->pleuralize (strtolower (get_class ($this))));
58
...
59 10 Mischa The Evil
</code></pre>
60
# and then changing the method @_send_and_receive@ to use @$sOriginalElementName@ instead of @substr ($this->element_name, 0, -1)@:
61
<pre><code class="php">
62 9 Robert Nürnberg
function _send_and_receive ($url, $method, $data = array ()) {
63
  $params = '';
64
  $el = $this->sOriginalElementName;//substr ($this->element_name, 0, -1);
65
  if ($this->request_format == 'url') {
66
...
67 10 Mischa The Evil
</code></pre>