■allow_url_fopen が有効の環境
//file_get_contentsを使う場合 (allow_url_fopen がTRUEの環境)
function send_to_discord($message) {
//
$webhook_url = 'https://discord.com/api/webhooks/XXXXXXXXXXXXXXXXXX/XXXX ~ XXXX';
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-Type: application/json',
'content' => json_encode($message),
)
);
$resp = file_get_contents($webhook_url, false, stream_context_create($options));
return $resp;
}
//メッセージの内容を定義
$contents = 'Test string';
$message = array(
'username' => 'My PHP Bot',
'content' => $contents
);
send_to_discord($message);
■allow_url_fopen が無効の環境
//curlを使う場合 (allow_url_fopen がFALSEの環境)
function send_to_discord($message) {
//
$webhook_url = 'https://discord.com/api/webhooks/XXXXXXXXXXXXXXXXXX/XXXX ~ XXXX';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $webhook_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($message));
$resp = curl_exec($ch);
curl_close($ch);
return $resp;
}
//メッセージの内容を定義
$contents = 'Test string';
$message = array(
'username' => 'My PHP Bot',
'content' => $contents
);
send_to_discord($message);
おまけ
■SecondLifeからLSLでPOSTする場合
SecondLifeのLSL(Linden Sscript Language)での場合は以前のBlog記事を参考にしてください。
・ローカルチャットをDiscordのチャンネルにPOSTする
・アバターのオンライン状況をDiscordのチャンネルにPOSTする
0 件のコメント:
コメントを投稿