前回はサイボウズAPIをPHPから利用して設備の予約状況を取得した。
今回は、施設IDが1の施設に対し2016年1月1日10時~11時の予約を入れてみる。
参加者はユーザーID100の社員とする。
sample.php(前半)
// 施設ID
$fid="1";
// 参加者ユーザID
$mid="100";
// 送信先
$url='http://(インストール先)/ag.cgi?page=PApiSchedule';
// ヘッダ
$headers = array(
'Content-Type: application/soap+xml; charset=UTF-8; action="ScheduleAddEvents"',
);
// リクエスト内容
$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">
ScheduleAddEvents</Action>
<Security xmlns="http://schemas.xmlsoap.org/ws/2002/12/secext">
<UsernameToken>
<Username>xxxxx</Username>
<Password>xxxxx</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:ScheduleAddEvents xmlns:tns="http://wsdl.cybozu.co.jp/base/2008">
<parameters>
<schedule_event
xmlns=""
id="dummy"
event_type="normal"
version="dummy"
public_type="public"
plan=""
detail="自動予約"
description="スクリプトから登録."
timezone="Asia/Tokyo"
end_timezone="Asia/Tokyo"
allday="true"
start_only="false">
<members>
<member>
<facility id="{$fid}"></facility>
</member>
<member>
<user id="{$mid}"></user>
</member>
</members>
<when>
<datetime start="2016-01-01T10:00:00" end="2016-01-01T11:00:00"></datetime>
</when>
</schedule_event>
</parameters>
</tns:ScheduleAddEvents>
</soapenv:Body>
</soapenv:Envelope>
EOF;
parameters内で最初のほうに出てくるidとversionは、予約登録時に自動で割り振られるため、リクエスト時にはdummyデータを入れている。
plan、detail、description、membersの中は任意に設定する。
<members>~</members>
の中が予約対象で、入れ子になっている<member>~</member>
を増やすことで複数の施設・参加者を登録できる。
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{
// 予約IDを表示する
if ($reader->name=="schedule_event") {
echo "予約しました。id:".$reader->getAttribute("id")."<br />";
}
}
}
$reader->close();
後半は取得の場合とほとんど変わらないが、レスポンスのXMLを読んで予約に成功したかをチェックしている。
予約idに値が入った=予約成功と判断する。