せかいや

いまいるここを、おもしろく http://sekai-in-the-box.appspot.com/

【PHP】【GAE】PHPでtwitterBot & GAEにデプロイ

PHPでのBot作成時の作業記録です。

GAE for PHP への登録

現在GAE for PHPは申請制をとっており、
https://gaeforphp.appspot.com/:tilteから申し込む必要があります。
承認され次第、Googleから「登録したよメール」が届きます(※1)。
#ちなみに、申請から数時間後にメールが来ました。
使用目的に「for Twitter Bot. It tweest Japanese beautiful poem (TANKA)」と書いたのが功を奏したのでしょうか。無害そうな感じ?

Introduction - Google App Engine — Google Developersの通りに進んでいきます。


 

ローカルホストにデプロイ

appserver.py --php_executable_path を実行します。

C:\Python27>C:\google_appengine\dev_appserver.py --php_executable_path=C:\xampp\
php\php-cgi.exe C:\xampp\htdocs\helloworld\
Allow dev_appserver to check for updates on startup? (Y/n): y
dev_appserver will check for updates on startup. To change this setting, edit C
:\Users\hoge_ADMIN/.appcfg_nag
INFO 2013-08-02 12:50:19,588 sdk_update_checker.py:244] Checking for updates
to the SDK.
INFO 2013-08-02 12:50:24,655 sdk_update_checker.py:260] Update check failed:
HTTP Error 404: Not Found
WARNING 2013-08-02 12:50:24,701 api_server.py:314] Could not initialize images
API; you are likely missing the Python "PIL" module.
WARNING 2013-08-02 12:50:24,740 simple_search_stub.py:998] Could not read searc
h indexes from c:\users\hoge_ad~1\appdata\local\temp\appengine.iroironashi\search
_indexes
INFO 2013-08-02 12:50:24,744 api_server.py:138] Starting API server at: http

//localhost
55901

INFO 2013-08-02 12:50:24,750 dispatcher.py:164] Starting module "default" ru
nning at: http://localhost:8080
INFO 2013-08-02 12:50:24,755 admin_server.py:117] Starting admin server at:
http://localhost:8000
INFO 2013-08-02 12:53:44,214 module.py:595] default: "GET /helloworld HTTP/1
.1" 200 13
INFO 2013-08-02 12:53:44,799 module.py:595] default: "GET /favicon.ico HTTP/
1.1" 200 13

■表示画面
 f:id:sekaiya:20130809122131j:plain


GAEへのアップロード

appcfg.py -R update を実行します

C:\google_appengine>appcfg.py -R update C:\xampp\htdocs\helloworld\
11:52 AM Host: appengine.google.com
11:52 AM Application: iroironashi; version: 1
11:52 AM
Starting update of app: iroironashi, version: 1
11:52 AM Getting current resource limits.
Email: MAILLLLL
Password for MAILLLLL:
11:52 AM Scanning files on local disk.
11:52 AM Cloning 2 application files.
11:52 AM Uploading 1 files and blobs.
11:52 AM Uploaded 1 files and blobs
11:52 AM Compilation starting.
11:52 AM Compilation completed.
11:52 AM Starting deployment.
11:52 AM Checking if deployment succeeded.
11:52 AM Deployment successful.
11:52 AM Checking if updated app version is serving.
11:52 AM Completed update of app: iroironashi, version: 1

■表示画面
f:id:sekaiya:20130809122205j:plain


 

cron の設定

作成したBOTが自動実行されるようにcronを設定します。
cron.yaml

cron:

  • description: tweet

url: /main.php
schedule: every 12 hours
timezone: Asia/Tokyo

 
再度デプロイすると、管理コンソールのcron欄に反映されます。
f:id:sekaiya:20130809122232j:plain


作成したBOT

12時間に1回、センスあふれる詩をつぶやきます。
青空文庫で採取した、著作権の切れた詩を使用しています。
http://twitter.com/iroironashi


 

作成したBOTのコード

https://github.com/sekaiya/iroironashiでソースコートが見れます


 
その他メモ

GAEではcurlが使えない

curl on app engine - Stack Overflowなど。
GAEではcurlが使えない模様。

https://github.com/abraham/twitteroauthからtwitteroauth.php をDLして使っていたのですが、
curlを使用しているためにエラーとなりました。
curl使わないコードを追加。下記参照。


 

twitteroauth.php をGAE上で使いたい場合

以下の通りコードを修正します。

<?php
 function http($url, $method, $postfields = null) {
  	if (function_exists('curl_init')) {
  		return $this->httpCurl($url, $method, $postfields);
  	}
  	return $this->httpNoCurl($url, $method, $postfields);
  }
  
  function httpNoCurl($url, $method, $postfields = null) {
	  $headers = array(
	  		'Content-Type: application/x-www-form-urlencoded',
	  		'Content-Length: '.strlen($postfields),
	  );
	  $options = array('http' => array(
	  		'method' => $method,
	  		'content' => $postfields,
	  		'header' => implode("\r\n", $headers),
	  ));
	  
	  $contents = file_get_contents($url, false, stream_context_create($options));
	  return $contents;
  }

これにておしまい。
 

※1

Congratulations!

We just wanted to let you know that your App Engine application:
iroironashi (Twitter Bot)
has been be successfully whitelisted to deploy PHP applications.

This means you can now deploy PHP projects to this application using
the appcfy.py tool available in the Google App Engine for PHP SDK.

While we're in Preview you can keep up to date with the latest news on
Google App Engine for PHP by following our team blog:
http://gaeforphp-blog.appspot.com/
(and yes, it's running WordPress)

You can also read our online documentation:
https://developers.google.com/appengine/docs/php/gettingstarted/
https://developers.google.com/appengine/docs/php/

If you're setting up a WordPress blog, don't forget our tutorial:
https://developers.google.com/appengine/articles/wordpress

You can get help from other App Engine developers on Stack Overflow:
http://stackoverflow.com/questions/tagged/google-app-engine+php

And if you find a bug, or would like to request a feature, you can
do so on our Issue Tracker:
https://code.google.com/p/googleappengine/issues/list?q=label:Language-PHP&sort=-stars

Thanks for being one of the first to try Google App Engine for PHP.

Enjoy! We can't wait to see what you come up with.

  • The App Engine Team