プロジェクト

全般

プロフィール

Rest api with php » 履歴 » バージョン 2

Mitsuyoshi Yoshida, 2011/11/15 23:41

1 1 Mitsuyoshi Yoshida
redmine.org の "Rest_api_with_php":http://www.redmine.org/projects/redmine/wiki/Rest_api_with_php の日本語訳です。
2
3
h1. PHP での REST API の使用法
4
5
PHP には "PHP ActiveResource":http://wiki.github.com/lux/phpactiveresource/ という Rails の REST API を使用するための軽量なライブラリがあります。
6
以下にその使用例を記述します。
7
8
<pre>
9
<code class="php">
10
<?php
11
require_once ('ActiveResource.php');
12
13
class Issue extends ActiveResource {
14
    var $site = 'http://username:password@192.168.199.129:3000/';
15
    var $request_format = 'xml'; // REQUIRED!
16
}
17
18
// チケットの作成
19
$issue = new Issue (array ('subject' => 'XML REST API', 'project_id' => '1'));
20
$issue->save ();
21
echo $issue->id;
22
23
// チケットの取得
24
$issues = $issue->find ('all');
25
for ($i=0; $i < count($issues); $i++) {
26
	echo $issues[$i]->subject;
27
}
28
29
// ID で指定したチケットの取得と更新
30
$issue->find (1);
31
echo $issue->subject;
32
$issue->set ('subject', 'This is the new subject')->save ();
33
34
// チケットの削除
35
$issue->find (1);
36
$issue->destroy ();
37
?>
38
</code>
39
</pre>
40
41
*既知の問題*
42
43
* かなり長い"説明"が記述されている場合には、 WEB サーバーは 417(Bad Expectation) エラーを返す可能性があります。
44
_ActiveResource.php_ の 381 行目を次のように書き直して下さい。
45
<pre><code class="php">curl_setopt ($ch, CURLOPT_HTTPHEADER, array ('Expect:',"Content-Type: text/xml", "Length: " . strlen ($params)));</code></pre>
46
47 2 Mitsuyoshi Yoshida
訳注: もう一つ不具合の対処法が記述されていましたが、今リリースされている ActiveResource.php のバージョンでは対応済みのようなので、翻訳していません。