実験メモ:いろいろな方法で statuses/update

同じことをするにも色々方法がある。

ということで、Twitterに発言するという同じことがどんなコードになるか、試してみた。

  1. Services_Twitter
  2. fopen
  3. file_get_contents
  4. cURL
  5. twitteroauth

1.Services_Twitter

<?php
require_once "Services/Twitter.php";
//ユーザー名、パスワード
$username='Twitterユーザー名';
$password='Twitterパスワード';
//メッセージ
$message='テストメッセージ Services_Twitter 利用です';

$st =& new Services_Twitter($username, $password);

$result = $st->setUpdate($message);

echo $result;

?>


2.fopen
 ※参考にさせていただきました ( PHPTwitterに投稿 http://d.hatena.ne.jp/hirataka522/20080126/1201300282 )

<?php
//ユーザー名、パスワード
$username='Twitterユーザ‐名';
$password='Twitterパスワード';

$message='テストメッセージ fopen 利用';

$ret= tweet_fop($message, $username, $password);
echo "結果=[".print_r($ret)."]<br>";

//fopenを利用したtweet function
function tweet_fop($message, $username, $password)
{
    $url = "http://twitter.com/statuses/update.xml?";
    $params = "status=". rawurlencode($message);

    $st = stream_context_create(array(
        "http" => array(
            "method" => "POST",
            "header" => "Authorization: Basic ". base64_encode($username. ":". $password)
        )
    ));

if(($fp = fopen($url.$params, 'r', false, $st)) == false){
      return(false);
}
$contents=stream_get_contents($fp);
fclose($fp);
return ($contents);

}
?>

3.file_get_contents
 ※参考にさせていただきました ( PHPTwitterに投稿 http://d.hatena.ne.jp/hirataka522/20080126/1201300282 )

<php
//ユーザー名、パスワード
$username='Twitterユーザ‐名';
$password='Twitterパスワード';

$message='テストメッセージ file_get_contents 利用';

$ret= tweet_fgetcon($message, $username, $password);
echo "結果=[".print_r($ret)."]<br>";

//file_get_contentsを利用したtweet function
function tweet_fgetcon($message, $username, $password){
  $context = stream_context_create(array(
    'http' => array(
    'method'  => 'POST',
    'header'  => sprintf("Authorization: Basic %s\r\n", base64_encode($username.':'.$password)).
      "Content-type: application/x-www-form-urlencoded\r\n",
    'content' => http_build_query(array('status' => $message)),
    'timeout' => 5,
    ),
  ));
  $ret = file_get_contents('http://twitter.com/statuses/update.xml', false, $context);
 
  return false !== $ret;
}
?>


4.cURL
 ※参考にさせていただきました ( たったこれだけでPHPからTwitterに投稿できる関数 http://www.multiburst.net/ElectricBrain/2009/07/function-of-php-to-tweet )

<php
//ユーザー名、パスワード
$username='Twitterユーザ‐名';
$password='Twitterパスワード';

$message='テストメッセージ cURL 利用';

$ret= tweet_bycURL($message, $username, $password);
echo "結果=[".print_r($ret)."]<br>";

//cURLを利用したtweet function
function tweet_bycURL($message, $username, $password)
{
    $url = 'http://twitter.com/statuses/update.json';
    $fld = http_build_query(array('status' => $message));
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERPWD, $username.':'.$password);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fld);
 
    $ret = curl_exec($ch);
 
     return false !== $ret;
}
?>

5.twitteroauth.php

<?php

//Consumer keyの値
$consumer_key = "コンシューマ・キー";
//Consumer secretの値
$consumer_secret = "コンシューマ・シークレット";
//Access Tokenの値
$access_token = "アクセス・トークン";
//Access Token Secretの値
$access_token_secret = "アクセス・トークン・シークレット";
 
require_once("./twitteroauth/twitteroauth.php"); 

    $message = 'テストメッセージ twitteroauthを利用';   

$connection = new TwitterOAuth($consumer_key,$consumer_secret,$access_token,$access_token_secret);

$result=$connection->post('statuses/update', array('status' => $message));

echo print_r($result);

?>