2012年10月26日金曜日

cakephp2 インターネット上の画像をサーバに保存する

実は、cakephpで、curlを使ってみたかっただけですが

cakephpネタでは、ないですが、インターネット上の画像をサーバに保存したい。
ということがあり、作って見ました。
本当は、wget関数とかがほしいですね
(ポイント)
・インターネット上の画像のURLは、$http_image
・サーバ上の画像のファイルを$local_imageで指定します。
・保存ディレクトリの名前に注意して、wb(ライト・バイナリ)モードで、オープンし、
・画像を読み込み、書き出す、だけです。

2012年10月25日木曜日

cakephp2 hybridauth(2)

hybridauth概要 その2/2です。

Hybrid_User_Profile Name type description
  identifier String  
Properties profileURL String IDPのWebサイト上でプロフィールページへのURLリンク
  webSiteURL String  
  photoURL String ユーザー写真やアバターへのURLリンク
  displayName String  
  description String 短いabout_me
  firstName String  
  lastName String  
  gender String  
  language String  
  age Integer  
  birthDay Integer  
  birthMonth Integer  
  birthYear Integer  
  email String  
  emailVerified String  
  phone String  
  address String  
  country String  
  region String  
  city String  
  zip Integer  
Update User Status      $hybridauth = new Hybrid_Auth( $config );
$adapter = $hybridauth->authenticate( "Facebook" );
$adapter->setUserStatus(
    array(
       "message" => "", // status or message content
       "link" => "", // webpage link
       "picture" => "", // a picture link
       )
    ); 
      $hybridauth = new Hybrid_Auth( $config );
$adapter = $hybridauth->authenticate( "Twitter" );
$adapter->setUserStatus( "Hi there!" ); 
Hybrid_User_Contact $hybridauth = new Hybrid_Auth( $config );
$adapter = $hybridauth->authenticate( "Twitter" );
$user_contacts = $adapter->getUserContacts();
foreach( $user_contacts as $contact ){
     echo $contact->displayName . " " . $contact->profileURL . "<hr />";
}
  Field Name Type Short description
  identifier  String The Unique contact's ID on the connected provider. Usually an interger.
  profileURL  String URL link to profile page on the IDp web site
  webSiteURL  String ユーザのウェブサイト、ブログ、ウェブページ
  photoURL  String ユーザー写真やアバターへのURLリンク
  displayName  String User dispalyName provided by the IDp or a concatenation of first and last name.
  description  String 短いabout_meまたは最後の接触状況
  email  String User email. Not all of IDp garant access to the user email
Hybrid_User_Activity $hybridauth = new Hybrid_Auth( $config );
$adapter = $hybridauth->authenticate( "Twitter" );
$user_timeline = $adapter->getUserActivity( "timeline" );
foreach( $user_timeline as $item ){
    echo $item->user->displayName . ": " . $item->text . "<hr />";
}

$user_timeline = $adapter->getUserActivity( "me" );
foreach( $user_timeline as $item ){
    echo $item->user->displayName . ": " . $item->text . "<hr />";
}
  Field Name Type Short description
  id  String Event id on the provider side
  date  String Event date of creation. provided as is for now.
  text  String activity/event/story content as string.
  user  stdClass Field Name    Type    Short description
------------------------------------------------------
identifier        string   The Unique user ID on the provider side. Usually an interger.
displayName string   User dispalyName provided by the provider
profileURL     string   URL link to profile page on the IDp web site
photoURL      string   URL link to user photo or avatar
Access social networks apis     try{
    $hybridauth = new Hybrid_Auth( $config );
    $facebook = $hybridauth->authenticate( "Facebook" );
    $twitter = $hybridauth->authenticate( "Twitter" );

    /* Facebook: https://developers.facebook.com/docs/reference/api/  */
    $response = $facebook->api()->api('/me/friends');
    $response = $facebook->api()->api("/me/feed", "post", array(
        message => "Hi there",
        picture => "http://www.mywebsite.com/path/to/an/image.jpg",
        link => "http://www.mywebsite.com/path/to/a/page/",
        name => "My page name",
       caption => "And caption"
       ));

     /* Twitter: https://dev.twitter.com/docs/api */
    $response = $twitter->api()->get( 'account/totals.json' );
   }
catch( Exception $e ){
    echo "Ooophs, we got an error: " . $e->getMessage();
}
HybridAuth Sessions         CREATE TABLE `users_connections` (
    `user_id` int(11) NOT NULL COMMENT 'refer to your user id on your application',
    `hybridauth_session` text NOT NULL COMMENT 'will contain the hybridauth session data',
    `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
    ) ENGINE=InnoDB;
      try{
$hybridauth = new Hybrid_Auth( $config );
$twitter = $hybridauth->authenticate( "Twitter" );
$facebook = $hybridauth->authenticate( "Facebook" );
$google = $hybridauth->authenticate( "Google" );
/* ... */

$hybridauth_session_data = $hybridauth->getSessionData();
 
// then store it on your database or something
sotre_hybridauth_session( $current_user_id, $hybridauth_session_data );
}
catch( Exception $e ){
    echo "Ooophs, we got an error: " . $e->getMessage();
}
 
// define a function to sotre it on whatever storage you want to use
function sotre_hybridauth_session( $user_id, $data ){
    $sql = "INSERT INTO users_connections ( user_id, hybridauth_session ) VALUES (  $user_id , $data )";
// ..
}
      try{
    $hybridauth = new Hybrid_Auth( $config );

    $hybridauth_session_data = get_sotred_hybridauth_session( $current_user_id );
    $hybridauth->restoreSessionData( $hybridauth_session_data );

   $twitter = $hybridauth->getAdapter( "Twitter" );

  $user_profile = $twitter->getUserProfile();
 
// ..
}
catch( Exception $e ){
   echo "Ooophs, we got an error: " . $e->getMessage();
}
 
// define a function to get the stored hybridauth data back from your storage system
function get_sotred_hybridauth_session( $user_id ){
    $sql = "SELECT FROM users_connections WHERE user_id = $user_id ";
    return ...
}

cakephp2 hybridauth(1)

hybridauthの概要 その1/2です。

Methods   Usage
Hybrid_Auth authenticate() Hybrid_Auth::authenticate( provider, params  ) 
     
  getAdapter() Hybrid_Auth :: isConnectedWith( provider )
  isConnectedWith() Hybrid_Auth :: isConnectedWith( provider )
    現在のユーザーが指定に接続されている場合はtrueまたはfalseを返す
  getConnectedProviders() Hybrid_Auth :: getConnectedProviders()
    認証プロバイダのリストを返します。
  getSessionData() Hybrid_Auth :: getSessionData()
    データが格納されたセッションを取得します。シリアライズ返す
  restoreSessionData() Hybrid_Auth :: restoreSessionData( array )
    与えられた[シリアライズ]からHybridAuthセッションをリストアする
  logoutAllProviders() Hybrid_Auth :: logoutAllProviders()
    一度に接続されているすべてのプロバイダをログアウトする汎用関数。
  getCurrentUrl() Hybrid_Auth :: getCurrentUrl()
    効用関数は、要求Webページの現在のURLを返す。
Hybrid_Provider_Adapter  各認証プロバイダのためにそれのインスタンスを作成 $twitter_adapter = $hybridauth->authenticate( "Twitter" );
$twitter_user_profile = $twitter_adapter->getUserProfile();
$twitter_adapter->logout(); 
  isUserConnected() Hybrid_Provider_Adapter ::(isUserConnected)
  認証されている場合にtrueを返します  
  getUserProfile() Hybrid_Provider_Adapter :: getUserProfile()
  setUserStatus() Hybrid_Provider_Adapter :: setUserStatus($ status)
  ソーシャルネットワーク上の彼のwallへの
ユーザー·ステータスまたはポストを更新
ソーシャルサービスに送信するユーザのステータス
  getUserContacts() Hybrid_Provider_Adapter :: getUserContacts()
  接続ユーザーの連絡先リストを提供  
  getUserActivity() Hybrid_Provider_Adapter::getUserContacts( $stream )
  彼の友人のアクティビティストリームを提供 ユーザのアクティビティをロードするため
  logout() Hybrid_Provider_Adapter::logout()
  getAccessToken() Hybrid_Provider_Adapter::getAccessToken()
    access.tokenプロバイダがOAuthプロトコルを使用している場合
  API() Hybrid_Provider_Adapter::api()

cakephp2.2.3 hybridautu + tmhOAuth レイアウト twitter用

cakephpでtwitterをやるには、hybridauthだけで、OKそうなんですが、
画像付きの投稿が出来ないので、その部分は、tmhOAthでやります。
「手順」
1.ダウンロード、配置
2.Twitterのアプリを作成
3.アクションを作成し、実行(試す)

cakephp2.2.3 + Hybridauth + tmhOAth のレイアウトです。
それぞれをダウンロードして、「だた」下記のように配置(コピー)してください。
コピーするだけでOKなので、解りやすいですね。

「ダウンロード」
(1)cakephp2.2.3をダウンロード
(2)Hybridauthのcakephp用をダウンロード
(3)tmhOAthの最新版をダウンロード
「配置」
(1)cakephpのwebrootにhybridauthをホルダーごとコピー
(2)cakephpのwebrootにtmhOAthをホルダーごとコピー

「ツイッターのdeveloper」にいって、アプリケーション作成し、登録する。
・ここは、他の解説?を見て下さい

「アクション作成」
(ポイント)
・require_onceは、下記の例のままでOK
・base_urlは、例のままでOK
・Twitterのkeyとsecretに、作成したアプリケーションのkeyとsecretを書き込む
・tmhOAth用に、consumer_keyとconsumer_secretのテーブルに作成したアプリのkeyとsecretを書き込む
・表示するイメージを./img/images/とかに準備しておく



(Hybridauthのcallbackの解説)
・$twitter = $hybridauth->authenticate('Twitter'); とするだけで、
 webroot/hybridauth/index.phpを実行、認証して、次の行に戻ってきます。

認証済みの場合は、素通りします。
めちゃ便利ですよね。

2012年10月21日日曜日

cakephp2.3 find conditions and条件、or条件

忘れてしまうので、メモ。

  $conditions = array(
  'or' => array(
    'or' => array(
     array("Schedule.from BETWEEN ? AND ?" => array($from, $to)),
     array("Schedule.to BETWEEN ? AND ?" => array($from, $to))
     ),
    'and' => array(
     array("Schedule.from >=" => $from),
     array("Schedule.from <=" => $to),
     array("Schedule.to     >=" => $to)
     ),
    'and' => array(
     array("Schedule.from <=" => $from),
     array("Schedule.to     <=" => $to),
     array("Schedule.to  >=" => $from)
     ),
    'and' => array(
     array("Schedule.from <=" => $from),
     array("Schedule.to     >=" => $to)
     )
    ),
  'and' => array(
    'SchedulesUser.user_id' => $user_id
     )
   );

2012年10月6日土曜日

windowsにvirtualboxでMacOSを動かす

とりあえず、動いたので、画面を紹介します。
使い方はまた、後日、メモします。