LoginSignup
1
0

More than 3 years have passed since last update.

Twitter API with PHP + WordPress の Tips

Last updated at Posted at 2020-10-15

Twitterまとめのまとめサイトを作りました (2ちゃんねるまとめのまとめを参考にしています)。このサイト作りから得たTipsをまとめます。

Twitter API 関連

Twitter APIの始め方 (WordPress/PHP環境)

別途、こちらの記事を参照ください (別サイトです)。

Twitter検索+時間指定

検索ワードにsinceとuntilを併せて指定することで、時間指定をかけたTwitter検索ができます (参考)。
下記の例では、現時刻から8時間前までのツイートを検索しています (9時間引いているのは、日本時間から協定世界時へ調整するためです)。

$dateImmu = new DateTimeImmutable();
$until = $dateImmu->modify('-9 hours')->format('Y-m-d_H:i:s') . '_UTC';
$since = $dateImmu->modify('-17 hours')->format('Y-m-d_H:i:s') . '_UTC'

$connection = getToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
$tweets = $connection->get("search/tweets",
                           ["q" => "since:" . $since . " until:" . $until,
                            "result_type" => "recent",
                            "lang" => "ja",
                            "count" => 100]);

PHP 関連

文章から最後のhttpsリンクを取り除く

下記のように、文章の最後にhttpsリンクがくっついているものを考えます。

【ニュース】『スマブラSP』に参戦した『マイクラ』スティーブの「股間」に注目集まる。ソレにしか見えない人々 https://t.co/XtIRzKfHpQ

このhttps表記だけを、文章から取り除きます。

【ニュース】『スマブラSP』に参戦した『マイクラ』スティーブの「股間」に注目集まる。ソレにしか見えない人々

$t = explode('https://t.co/', $text);
if (count($t) == 1) {
  $t = $t[0];
} else {
  $t = implode(array_slice($t, 0, count($t)-1));
}

WordPress / HTML 一般

フォルダの場所

WordPressの「外観→テーマの編集」のファイルが入っているフォルダは、/home/[サーバーID]/[独自ドメイン名]/public_html (Xserverの場合)。

functions.phpを直接編集したい、なんて思ったときに。

<?php
echo getcwd() . "\n";
?>

サーバー上ファイルの編集方法

SCPでサーバー上のファイルをローカルにコピーをして、編集後にそれをSCPコマンドでサーバーへ返すことができる。SCPを使うためには、SSHが利用可能でないといけない。準備方法はこちら (別サイトです)。

viが嫌いな人向け。

これでサーバー→ローカルへのコピー。

scp -i [xxx.key] -P 10022 [サーバーID]@[ホスト名]:/home/[サーバーID]/[独自ドメイン名]/public_html/wp/wp-content/themes/[テーマ名]/functions.php ./

これでローカル→サーバーへのコピー。

scp -i [xxx.key] -P 10022 ./functions.php [サーバーID]@[ホスト名]:/home/[サーバーID]/[独自ドメイン名]/public_html/wp/wp-content/themes/[テーマ名]/functions.php

画面遷移なしの検索フォーム

formで自ブロック内のidを指定して上げれば、擬似的に画面遷移なしで検索ができる。

<h2> <a id='search'> 検索ポイント </a> </h2>
<form action = "#search" method = "get">
<input type = "text" name = "query" class="textbox">
<input type = "submit" value ="検索" class="btn-submit">
</form>

<?php   
if(isset($_GET['query'])){
  echo "You searched " . $_GET['query'];

} else {
  echo "No Input";
}
?>
1
0
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
1
0