PHPでTwitterのプロフィール情報の一部を他のアカウントと同期する

Twitterで複数アカウント使ってる場合に、プロフィール画像 等を一緒に変更するのが面倒くさいので、PHPスクリプトをCRONで叩けば同期できるようにしてみました。
TwitterAPIが必要なため、事前に承認作業をしとく必要があります。

TwitterOAuthでは画像のPOSTが難しい?らしく、ググッて引っかかったUltimateOAuthを使用させていただくことにしました。

[UltimateOAuth]
 https://github.com/danielsum/UltimateOAuth/

コードは以下。

<?php

$consumer_key = " "; // Consumer keyの値
$consumer_secret = " "; // Consumer secretの値
$access_token = " "; // Access Tokenの値
$access_token_secret = " "; // Access Token Secretの値

$ultimateoauth  =   "UltimateOAuth.php";
require_once($ultimateoauth);

// OAuthオブジェクト生成
$uo = new UltimateOAuth($consumer_key,$consumer_secret,$access_token,$access_token_secret);

$result = $uo->OAuthRequest(
    "https://api.twitter.com/1.1/users/show.json",
    "GET",
    array( "user_id"=>"*********" ) //プロフィール取得対象のユーザID
    );

/*** プロフィールイメージの処理 ***/
//大きいサイズのプロフィールイメージURLを取得
$profileImageUrl = str_replace('_normal', '', $result->profile_image_url );
$profileImage = file_get_contents( $profileImageUrl );
$uo->OAuthRequest(
    "https://api.twitter.com/1.1/account/update_profile_image.json",
    "POST",
    array('image' => base64_encode($profileImage) )
    );


/*** バックグラウンドイメージの処理 ***/
$profile_banner_image = file_get_contents( $result->profile_banner_url );
$uo->OAuthRequest(
    "https://api.twitter.com/1.1/account/update_profile_banner.json",
    "POST",
    array('banner' => base64_encode($profile_banner_image) )
    );


/*** プロフィールメッセージの処理 ***/
$uo->OAuthRequest(
    "https://api.twitter.com/1.1/account/update_profile.json",
    "POST",
    array('description' => $result->description )
    );

コメント