LoginSignup
8

More than 5 years have passed since last update.

cybozu soap APIを叩いて、自分の本日のスケジュールを取得するサンプル(PHP)

Posted at
sample.php

$username='{cybozuのユーザー名}';
$password='{cybozuのパスワード}';

$today = date('Y-m-d');
$todayFrom = $today.'T00:00:00Z';
$todayTo   = $today.'T23:59:59Z';

// 送信先
$url='https://{サブドメイン名}.cybozu.com/g/cbpapi/schedule/api.csp';

// ヘッダ
$headers = [
    'Content-Type: application/soap+xml; charset=UTF-8',
    'action="ScheduleGetEvents"',
];

// リクエスト内容
$postdata =<<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
  <soap:Header>
    <Action>ScheduleGetEvents</Action>
    <Security>
      <UsernameToken>
        <Username>{$username}</Username>
        <Password>{$password}</Password>
      </UsernameToken>
    </Security>
    <Timestamp>
      <Created>2013-06-01T00:00:00Z</Created>
      <Expires>2037-12-31T00:00:00Z</Expires>
    </Timestamp>
  </soap:Header>
  <soap:Body>
    <ScheduleGetEvents>
      <parameters start="{$todayFrom}" end="{$todayTo}">
      </parameters>
    </ScheduleGetEvents>
  </soap:Body>
</soap:Envelope>
EOF;


$curl = curl_init( $url );
curl_setopt( $curl, CURLOPT_POST, TRUE );
curl_setopt( $curl, CURLOPT_POSTFIELDS, $postdata);
curl_setopt( $curl, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true); // curl_execの結果を文字列で返す
$contents = curl_exec( $curl );
curl_close($curl);


$reader = new XMLReader();
$reader->XML($contents);

// 結果出力
// あとはDOM操作なので、適当にお好みの形に整形してください。
$scheduleArr = [];
while($reader->read()) {

    if( $reader->nodeType == XMLReader::ELEMENT &&
            $reader->name == 'schedule_event' &&
            $reader->getAttribute("event_type") != "banner")
    {
        $scheduleItem = [];
        $doc = new DOMDocument('1.0', 'UTF-8');
        $scheduleEvent = simplexml_import_dom($doc->importNode($reader->expand(),true));
        //echo print_r($scheduleEvent,1);
        if( $scheduleEvent['public_type'] == "public" ) {
            $scheduleItem['detail'] = (string)($scheduleEvent['detail']);
        }
        else {
            $scheduleItem['detail'] = "予定あり";
        }

        if( $scheduleEvent['event_type'] == "repeat" ) {
            $scheduleItem['start'] = (string)($scheduleEvent->repeat_info->condition['start_time']);
            $scheduleItem['end']   = (string)($scheduleEvent->repeat_info->condition['end_time']);
        }
        else if( $scheduleEvent['event_type'] == "normal" ) {
            $scheduleItem['start'] = strftime( '%T', strtotime( $scheduleEvent->when->datetime['start'] ) );
            $scheduleItem['end']   = strftime( '%T', strtotime( $scheduleEvent->when->datetime['end'] ) );
        }
        $scheduleArr[] = $scheduleItem;
    }
}
$reader->close();

/**
scheduleArrが
[
    [
        "detail" => "予定タイトル",
        "start"  => "開始時間",
        "end"  => "終了時間",
    ]
]
みたいな形になります。
*/

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
8