忍者ブログ
2024.06│ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
とりあえずtwitter botからPHPの世界に突入したいです。
2024年06月30日 (Sun)
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

2009年05月12日 (Tue)

さて、前回とその前で、「ランダムな言葉をPOST」と、「時報をPOST」の方法を会得しました。
次はその2つを組み合わせてみます。

つまり「時報を含むランダムな言葉をPOST。」

2件のソースを見た目上ほぼ同じ使い方をすると、こうなります。
 

<?php
$url = "http://twitter.com/statuses/update.xml?";
$username = "rishitest";
$password = "password(ダミー)";

$mes[0] = "おはよう。もうG時ですよ。";
$mes[1] = "こんにちは。今はG時です。";
$mes[2] = "G時になった。いただきます。";
$mes[3] = "こんばんは。G時をまわってます。";
$mes[4] = "G時なので、もうおやすみなさい。";

$message = date($mes[rand(0, 4)]);

$params = "status=". rawurlencode($message);

$result = file_get_contents($url.$params , false, stream_context_create(array(
 "http" => array( "method" => "POST", "header" => "Authorization: Basic ".base64_encode($username. ":". $password)
 )
)));
?>


「$mes[0~4]」に文字列を入れるのはOKかと思われます。
で、このあとなんですが、そのまま「$message」に入れるのではなく、dete関数を使って返却された文字列を入れます。
こうすることによって、文字列のGの部分には時刻が入るというわけです。

http://twitter.com/rishitest/status/1773178421

さてここでひとつ補足ですが、date関数を使うと、date関数で置き換えられる文字はすべて日付や時間に置き換えられてしまいます。
例えば、以下のプログラム。

<?php
$url = "http://twitter.com/statuses/update.xml?";
$username = "rishitest";
$password = "password(ダミー)";

$mes[0] = "おはよう。もうG時ですよ。MySweetHoney。";
$mes[1] = "こんにちは。今はG時です。MySweetHoney。";
$mes[2] = "G時になった。いただきます。MySweetHoney。";
$mes[3] = "こんばんは。G時をまわってます。MySweetHoney。";
$mes[4] = "G時なので、もうおやすみなさい。MySweetHoney。";

$message = date($mes[rand(0, 4)]);

$params = "status=". rawurlencode($message);

$result = file_get_contents($url.$params , false, stream_context_create(array(
 "http" => array( "method" => "POST", "header" => "Authorization: Basic ".base64_encode($username. ":". $password)
 )
)));
?>


いちいち、MySweetHoneyって言わないと気がすまないとします。
しかしこれを実行すると、

http://twitter.com/rishitest/status/1773334100

文字が置き換えられまくって、悲惨な結果に…。
そうならないようにしたのが下のソースです。

<?php
$url = "http://twitter.com/statuses/update.xml?";
$username = "rishitest";
$password = "password(ダミー)";

$hour = date("G");

$mes[0] = "おはよう。もう".$hour."時ですよ。MySweetHoney。";
$mes[1] = "こんにちは。今は".$hour."時です。MySweetHoney。";
$mes[2] = $hour."時になった。いただきます。MySweetHoney。";
$mes[3] = "こんばんは。".$hour."時をまわってます。MySweetHoney。";
$mes[4] = $hour."時なので、もうおやすみなさい。MySweetHoney。";


$message = $mes[rand(0, 4)];

$params = "status=". rawurlencode($message);

$result = file_get_contents($url.$params , false, stream_context_create(array(
 "http" => array( "method" => "POST", "header" => "Authorization: Basic ".base64_encode($username. ":". $password)
 )
)));
?>


これはまず最初に変数「$hour」に時刻を入れてしまってから、それを文字列連結させたものをそのまま「$message」に入れるという手順です。
「$mes[0~4]」をdate関数に入れることはないので、「MySweetHoney」はちゃんとそのまま出力されます。

http://twitter.com/rishitest/status/1773359071

めでたし。
こんなかんじで、いろんな書き方ができます。
PR
この記事にコメントする
お名前
タイトル
文字色
メールアドレス
URL
コメント
パスワード   Vodafone絵文字 i-mode絵文字 Ezweb絵文字
この記事へのトラックバック
この記事にトラックバックする:
←No.9No.8No.7No.6No.5No.4
カレンダー
05 2024/06 07
S M T W T F S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
プロフィール
HN:
リシ
性別:
非公開
自己紹介:
環境はWindowsXP HOME SP3です。
最新コメント
[12/23 luisapape]
[12/23 margerieah]
最新トラックバック
フリーエリア
バーコード
ブログ内検索
アーカイブ
P R
アクセス解析