LoginSignup
10
15

More than 5 years have passed since last update.

サイボウズAPIを使ってPHPで施設の予約内容を取得する

Last updated at Posted at 2016-10-22

APIの概要は
「Garoon(ガルーン) API」
https://cybozudev.zendesk.com/hc/ja/categories/200157760-Garoon-API
として公開されているが、具体的なコードが少ないのでメモ。

今回は施設IDが1の設備について、2016年1月1日の予約内容を取得する。

※取りあえず動くことを目的としているので「ここがおかしい」「こうしたほうがよい」などあればお知らせください。

sample.php(前半)
// 施設ID
$id='1';

// 送信先
$url='http://(インストール先)/ag.cgi?page=PApiSchedule';

// ヘッダ
$headers = array(
'Content-Type: application/soap+xml; charset=UTF-8; action="ScheduleGetEventsByTarget"',
);

// リクエスト内容
$postdata =<<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Header>
<Action xmlns="http://schemas.xmlsoap.org/ws/2003/03/addressing">
ScheduleGetEventsByTarget</Action>
<Security xmlns="http://schemas.xmlsoap.org/ws/2002/12/secext">
<UsernameToken>
<Username>xxxxxx</Username>
<Password>xxxxxx</Password>
</UsernameToken>
</Security>
<Timestamp xmlns="http://schemas.xmlsoap.org/ws/2002/07/utility">
<Created>2013-06-01T00:00:00Z</Created>
<Expires>2037-12-31T00:00:00Z</Expires>
</Timestamp>
</soapenv:Header>
<soapenv:Body>
<tns:ScheduleGetEventsByTarget xmlns:tns="http://wsdl.cybozu.co.jp/base/2008">
<parameters start="2016-01-01T00:00:00" end="2016-01-01T23:59:00">
<facility id="{$id}"></facility>
</parameters>
</tns:ScheduleGetEventsByTarget>
</soapenv:Body>
</soapenv:Envelope>
EOF;

UsernameとPasswordには、予約内容を閲覧できる権限のユーザーと、そのパスワードを指定しておく。

ミソは

sample.php(前半・抜粋)
<parameters start="2016-01-01T00:00:00" end="2016-01-01T23:59:00">
<facility id="{$id}"></facility>
</parameters>

の部分で、APIマニュアルの「リクエスト」の内容がこれに当る。

続けて、POSTで送るデータを生成して実行する。

sample.php(後半)
// 送信内容生成
$options=array('http' => array(
'method'=>'POST',
'content'=>$postdata,
'header'=>implode("\n",$headers),
));

// 送信して、結果を格納
$contents = file_get_contents($url,false,stream_context_create($options));
$reader = new XMLReader();
$reader->XML($contents);

// 結果出力
while($reader->read()) {
    if($reader->nodeType == XMLReader::SIGNIFICANT_WHITESPACE || $reader->nodeType == XMLReader::END_ELEMENT){
        continue;
    }else{  
        // 予約のタイトルを表示する
        if ($reader->name=="schedule_event") {
            echo $reader->getAttribute("detail")."<br />";
        }
        // ほかにも表示するならここに追加する

    }
}
$reader->close();

結果のXMLが$contentsに代入されるので、XMLReaderで読み出して表示している。

$reader->getAttribute("start")に予約開始時刻が格納されているが、環境によってはタイムゾーンを調整する必要があるかもしれない。

そのほかに表示できる内容は
https://cybozudev.zendesk.com/hc/ja/articles/202463250#step1
に掲載されている。

例えば参加者も表示するには

sample.php(追加)
if ($reader->name=="user") {
    echo $reader->getAttribute("name")."<br />";
}

などとする。

10
15
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
15