プロジェクト

全般

プロフィール

操作

redmine.org の Rest_api_with_php の日本語訳です。

PHP での REST API の使用法

PHP には PHP ActiveResource という Rails の REST API を使用するための軽量なライブラリがあります。
以下にその使用例を記述します。

<?php
require_once ('ActiveResource.php');

class Issue extends ActiveResource {
    var $site = 'http://username:password@192.168.199.129:3000/';
    var $request_format = 'xml'; // REQUIRED!
}

// チケットの作成
$issue = new Issue (array ('subject' => 'XML REST API', 'project_id' => '1'));
$issue->save ();
echo $issue->id;

// チケットの取得
$issues = $issue->find ('all');
for ($i=0; $i < count($issues); $i++) {
    echo $issues[$i]->subject;
}

// ID で指定したチケットの取得と更新
$issue->find (1);
echo $issue->subject;
$issue->set ('subject', 'This is the new subject')->save ();

// チケットの削除
$issue->find (1);
$issue->destroy ();
?>

既知の問題

  • かなり長い"説明"が記述されている場合には、 WEB サーバーは 417(Bad Expectation) エラーを返す可能性があります。
    ActiveResource.php の 381 行目を次のように書き直して下さい。
    curl_setopt ($ch, CURLOPT_HTTPHEADER, array ('Expect:',"Content-Type: text/xml", "Length: " . strlen ($params)));

訳注: もう一つ不具合の対処法が記述されていましたが、今リリースされている ActiveResource.php のバージョンでは対応済みのようなので、翻訳していません。


Updated by Mitsuyoshi Yoshida , 12年以上前に更新
Access count: 11829 :since 2009-10-30

Mitsuyoshi Yoshida さんが12年以上前に更新 · 2件の履歴