ホーム > ブログ > タグ : PHP

cronジョブからCakePHPのアクションを実行する方法です。

1. まずはcronジョブがキックするスクリプトを作成します。

webroot/files/kickme.php
<?php
  $_GET['url'] = "hoges/add";
  require_once( dirname( dirname(__FILE__) ) . "/index.php" );
?>

上記の例では、hogesコントローラのaddアクションを実行します。

※kickme.phpには実行権限を与えてください。

2.cronジョブを設定します。

crontab -e
0 4 * * * php -f /path/to/app/webroot/files/kickme.php

上記の例では毎日4時0分にkickme.phpを実行します。

以上です。

cronがキックするスクリプトを公開ディレクトリに置きたくない場合は、
非公開ディレクトリに移してrequire_onceのパス指定を調整してください。

CakePHPでTwitterのOAuth認証を利用するライブラリが公開されています。

http://code.42dh.com/oauth/

上記サイトからコンポーネントファイルをダンロードして、
vendorsディレクトリ下に配置します。

以下、OAuth認証後につぶやきを投稿するサンプルコードです。
(コードは上記サイトのExampleより引用しています。)

app/controllers/example_controller.php

App::import('Vendor', 'oauth', array('file' => 'OAuth'.DS.'oauth_consumer.php'));

class ExampleController extends AppController {
    public $uses = array();
    
    public function twitter() {
        $consumer = $this->createConsumer();
	$requestToken = $consumer->getRequestToken('http://twitter.com/oauth/request_token', 'http://test.localhost/example/twitter_callback');
        $this->Session->write('twitter_request_token', $requestToken);
        $this->redirect('http://twitter.com/oauth/authorize?oauth_token=' . $requestToken->key);
    }
		      
    public function twitter_callback() {
        $requestToken = $this->Session->read('twitter_request_token');
	$consumer = $this->createConsumer();
	$accessToken = $consumer->getAccessToken('http://twitter.com/oauth/access_token', $requestToken);
		  
	$consumer->post($accessToken->key, $accessToken->secret, 'http://twitter.com/statuses/update.json', array('status' => 'hello world!'));
    }
	
    private function createConsumer() {
        return new OAuth_Consumer('YOUR_CONSUMER_KEY', 'YOUR_CONSUMER_SECRET');
    }
}

ライブラリの作者様に感謝します。

画像アップロードライブラリ「class.upload.php」の使用方法です。

以下は、オリジナル画像をアップロードして、S、M、Lの3サイズの画像を生成する例です。

require 'class.upload.php';

$file_name = 'hoge';
$upload_dir = '/path/to/upload/';

$handle = new Upload($_FILES['image_file']);
	
if ($handle->uploaded) {
	// Sサイズ画像
	$handle->allowed = array('image/*'); // 画像のみ許可
	$handle->image_convert = 'jpg'; // jpgに変換
	$handle->file_overwrite = true; // 上書き許可
	$handle->file_auto_rename = false; // 自動リネーム禁止
	$handle->file_src_name_body = $file_name . "_S"; // ファイル名
	$handle->image_resize = true; // リサイズ許可
	$handle->image_ratio = true; // 縦横比維持
	$handle->image_x = 100; // 横最大値
	$handle->image_y = 100; // 縦最大値
	$handle->image_ratio_no_zoom_in = true; // image_x、image_yより小さいサイズは拡大禁止	
	$handle->Process($upload_dir);
	
	// Mサイズ画像
	$handle->allowed = array('image/*'); // 画像のみ許可
	$handle->image_convert = 'jpg'; // jpgに変換
	$handle->file_overwrite = true; // 上書き許可
	$handle->file_auto_rename = false; // 自動リネーム禁止
	$handle->file_src_name_body = $file_name . "_M"; // ファイル名
	$handle->image_resize = true; // リサイズ許可
	$handle->image_ratio = true; // 縦横比維持
	$handle->image_x = 200; // 横最大値
	$handle->image_y = 200; // 縦最大値
	$handle->image_ratio_no_zoom_in = true; // image_x、image_yより小さいサイズは拡大禁止
	$handle->Process($upload_dir);
	
	// Lサイズ画像
	$handle->allowed = array('image/*'); // 画像のみ許可
	$handle->image_convert = 'jpg'; // jpgに変換
	$handle->file_overwrite = true; // 上書き許可
	$handle->file_auto_rename = false; // 自動リネーム禁止
	$handle->file_src_name_body = $file_name . "_L"; // ファイル名
	$handle->image_resize = true; // リサイズ許可
	$handle->image_ratio = true; // 縦横比維持
	$handle->image_x = 500; // 横最大値
	$handle->image_y = 500; // 縦最大値
	$handle->image_ratio_no_zoom_in = true; // image_x、image_yより小さいサイズは拡大禁止		
	$handle->Process($upload_dir);
	
	if (!$handle->processed) return $handle->error;

} else {
	 return $handle->error;
}

設定できるプロパティはたくさんあります。
詳しくは、class.upload.phpのサンプルページをご覧ください。