LoginSignup
3
1

More than 3 years have passed since last update.

Twitterの内容をDiscordにPHPで投稿する

Posted at

Twitterの内容をDiscordにPHPで投稿する

事前準備

<?php

require "twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;

$webhookurl = "https://discordapp.com/api/webhooks/~~~~~~";

$connection = new TwitterOAuth('~~~~~~~', '~~~~~~', '~~~~~~', '~~~~~~');
$content = $connection->get("account/verify_credentials");
$tweets = $connection->get("search/tweets", ["q" => "Youtube AND #cat OR #dog OR #animal filter:videos", "exclude_replies" => true, "count" => 1])->statuses;

foreach ($tweets as $tweet) {
  $json_data = array ('content'=>"$tweet->text");
  $make_json = json_encode($json_data);

  $ch = curl_init( $webhookurl );
  curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
  curl_setopt( $ch, CURLOPT_POST, 1);
  curl_setopt( $ch, CURLOPT_POSTFIELDS, $make_json);
  curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt( $ch, CURLOPT_HEADER, 0);
  curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);

  $response = curl_exec( $ch );
}
?>

あとはcron等で上記スクリプトを叩く

3
1
0

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
3
1