- var_dump: documentation ( source)
- json_decode: documentation ( source)
- http_build_query: documentation ( source)
- header: documentation ( source)
<?php
// 投稿先とAPIトークンを指定
$room_id = yyyyyy; // ルームID。URLからも確認できます
$api_token = "******************"; // APIトークン
// メッセージ本文
$body = <<<EOD
テストメッセージでござる。
このメッセージはChatwork APIにより自動で投稿されてるでござるよ。
EOD;
// ヘッダ
header("Content-type: text/html; charset=utf-8");
// POST送信データ
$params = array(
'body' => $body
);
// cURLに渡すオプションを設定
$options = array(
CURLOPT_URL => "https://api.chatwork.com/v1/rooms/{$room_id}/messages", // URL
CURLOPT_HTTPHEADER => array('X-ChatWorkToken: '. $api_token), // APIトークン
CURLOPT_RETURNTRANSFER => true, // 結果を文字列で返す
CURLOPT_SSL_VERIFYPEER => false, // サーバー証明書の検証を行わない
CURLOPT_POST => true, // HTTP POSTを実行
CURLOPT_POSTFIELDS => http_build_query($params, '', '&'), // POST送信データ
);
// cURLセッションを初期化
$ch = 1;
// cURL転送用の複数のオプションを設定
//curl_setopt_array($ch, $options);
// cURLセッションを実行
//$response = curl_exec($ch);
// cURLセッションをクローズ
//curl_close($ch);
// 結果のJSON文字列をデコード
$result = json_decode($response);
// 結果を出力 (メッセージID返ってきてる)
var_dump($result);
?>