C# [HttpGet] public async Task>で出力をしようとしてるのにエラー

よくわかっていませんが

<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.</ExceptionMessage><ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace/>
<InnerException>
<Message>An error has occurred.</Message>
<ExceptionMessage>型 '<>f__AnonymousTypeb`3[System.Int32,System.Int32,System.String]' をシリアル化できません。この型を DataContractAttribute 属性でマークするか、シリアル化するすべてのメンバーを DataMemberAttribute 属性でマークしてください。この型がコレクションの場合は、CollectionDataContractAttribute 属性でマークすることを検討してください。その他のサポートされている型については、Microsoft .NET Framework のマニュアルを参照してください。</ExceptionMessage>

このメッセージはFirefoxで直接アクセスしたから
直接アクセスするのはInternetExplorerで実行すれば言われない


ForefoxのAcceptヘッダー

text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

InternetExplorerのAcceptヘッダー

text/html, application/xhtml+xml, */*

application/xmlが邪魔するみたい。。。。

ので、FirefoxでもAcceptヘッダーからapplication/xmlを削除すればなんとかなります。





orz..

phpで503を出すには・・

何かしらの障害でサイトやページが出せなくなったら
取り急ぎ503で逃げることで
Googleクローラーが来ても「ぁぁメンテ中なのね」と思ってくれる
その間に頑張って復旧しよう!


page503.php

<?php
header ('HTTP/1.0 503 Service Temporarily Unavailable');
?>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

現在メンテナンス中です。<br>
誠に申し訳ございません。<br>


/やディレクトリ配下に以下.htaccessを置く(mod_rewriteやhtaccessが有効になってるのが前提)

.htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /page503.php [L]
</IfModule>


ファイル1つをアップロードで対応できるphpとかは楽だなーってトラブった時に思う次第

IBM BlueMixでCakePhpを動かす

性分的にオープンβもクローズドβも二つ返事で「やります」と言ってしまうwので、
IBM BlueMixというPaaSを使ってみることに。。


個人的にはphpさえあれば満足な人なので、
ひとまずCakePHPを動かすのを目標にしてみました。
(なぜかBlueMixでCakePHPを動かしたって記事をググっても見当たらないし・・)



なにはともあれ、BlueMixのアカウントを作成します。

IBM BlueMix
https://ace.ng.bluemix.net/


次にCreate An Applicationします。
その際に「Library for Java」を選んでおきます。
(って言うのもBlueMixではPHPはサポートしてないwけど、
 phpのBuildPackがあるのでそれを使います)







で、Applicationのコンテナができるのでここにプログラムを入れるんですが
プログラムをアップロードするのはCloudFoundryが提供しているcliでデプロイします


https://github.com/cloudfoundry/cli/releases
から自分は以下をダウンロードしてインストールしました


Installers
Mac OS X 64 bit



これでcfコマンドが使えるようになるのでセットアップします

$ cf api https://api.ng.bluemix.net
Setting api endpoint to https://api.ng.bluemix.net...
OK

API endpoint: https://api.ng.bluemix.net (API version: 2.2.0)
Not logged in. Use 'cf login' to log in.

loginはBlueMixのIBM IDのログインパスワード

$ cf login
API endpoint: https://api.ng.bluemix.net

Email> xxxxxx@xxxxxx.com

Password> 
Authenticating...
OK

Targeted org xxxxxx@xxxxxx.com

Targeted space dev


API endpoint: https://api.ng.bluemix.net (API version: 2.2.0)
User:         xxxxxx@xxxxxx.com
Org:          xxxxxx@xxxxxx.com
Space:        dev

ひとまずHelloしましょう

$ mkdir bluemixhello
$ cd bluemixhello/
$ vi index.php
---
<?php
echo "hello world";
---


作ったhello worldアプリ(w)をBlueMixにデプロイします
デプロイする際はcf-php-build-packを使います

$ cf push -b https://github.com/dmikusa-pivotal/cf-php-build-pack.git imaiworks1
Updating app imaiworks1 in org xxxxx@xxxxxxxx.com / space dev as xxxxx@xxxxxxxx.com...
OK

Uploading imaiworks1...
Uploading app files from: /Users/imaiworks/bluemixhello
Uploading 159, 1 files
OK

Stopping app imaiworks1 in org xxxxx@xxxxxxxx.com / space dev as xxxxx@xxxxxxxx.com...
OK

Starting app imaiworks1 in org xxxxx@xxxxxxxx.com / space dev as xxxxx@xxxxxxxx.com...
-----> Downloaded app package (4.0K)
OK
-----> Downloaded app buildpack cache (12M)
Cloning into '/tmp/buildpacks/cf-php-build-pack'...
Installing HTTPD
Installing PHP
Finished: [2014-06-29 04:33:39.205132]
-----> Uploading droplet (12M)

1 of 1 instances running

App started

Showing health and status for app imaiworks1 in org xxxxx@xxxxxxxx.com / space dev as xxxxx@xxxxxxxx.com...
OK

requested state: started
instances: 1/1
usage: 512M x 1 instances
urls: imaiworks1.mybluemix.net

     state     since                    cpu    memory          disk   
#0   running   2014-06-29 01:33:48 PM   0.0%   47.3M of 512M   34.2M of 1G  

これでBlieMixに上がったのでブラウザで確認します。

死ぬほど簡単。。



次は本題のCakePHP


ここでちょっとぶつかったのですが
cf-php-build-packのDocumentRootがhtdocsで
CakePHPは概ねapp/webrootなのでできればこれにしたい
ディレクトリに入れるなら別にそんなことしなくてもいいけどね)
ので、いろいろ試してこれかな?って思ったのは
cf-php-build-packのdefaults/config/httpd/2.4.x/httpd.conf内の

     DocumentRoot "${HOME}/htdocs"

     DocumentRoot "${HOME}/htdocs/app/webroot"

と書き換えることと、


cakephpのrootにあるindex.php

     define('CAKE_CORE_INCLUDE_PATH', ROOT . DS . 'lib');

     define('CAKE_CORE_INCLUDE_PATH', ROOT . DS . '../lib');

としたこと。


改変したbuilpackは
https://github.com/imaiworks/cf-php-build-pack-cakephp
にあげてありますのでご自由に・・


ってことでCakePHPの準備をします
cakephp-2.5.2.tar.gzをダウンロード

tar xzvf cakephp-2.5.2.tar.gz
cd cakephp-2.5.2
vi index.php
---
-        define('CAKE_CORE_INCLUDE_PATH', ROOT . DS . 'lib');
+        define('CAKE_CORE_INCLUDE_PATH', ROOT . DS . '../lib');
---

(SaltやDB設定は個々にやっておいてくださいまし)


BlueMixにデプロイします

$ cf push -b https://github.com/imaiworks/cf-php-build-pack-cakephp.git imaiworks1
Updating app imaiworks1 in org xxxxx@xxxxxxxx.com / space dev as xxxxx@xxxxxxxx.com...
OK

Uploading imaiworks1...
Uploading app files from: /Users/imaiworks/bluemix/cakephp-2.5.2
Uploading 5.5M, 1440 files
OK

Stopping app imaiworks1 in org xxxxx@xxxxxxxx.com / space dev as xxxxx@xxxxxxxx.com...
OK

Starting app imaiworks1 in org xxxxx@xxxxxxxx.com / space dev as xxxxx@xxxxxxxx.com...
OK
-----> Downloaded app package (2.1M)
-----> Downloaded app buildpack cache (12M)
Cloning into '/tmp/buildpacks/cf-php-build-pack-cakephp'...
Installing HTTPD
Installing PHP
Finished: [2014-06-29 04:51:29.022169]
-----> Uploading droplet (13M)

1 of 1 instances running

App started

Showing health and status for app imaiworks1 in org xxxxx@xxxxxxxx.com / space dev as xxxxx@xxxxxxxx.com..
OK

requested state: started
instances: 1/1
usage: 512M x 1 instances
urls: imaiworks1.mybluemix.net

     state     since                    cpu    memory          disk   
#0   running   2014-06-29 01:51:41 PM   0.0%   59.3M of 512M   46.5M of 1G   

たぶんこれでうまくいくはずです




cfを知らないので多くは語れないですが、
BlueMixはGoogle AppEngineとかHeroku的な感覚でした。
AWSだとAWS Elastic Beanstalkに近いですね。
今今動いているシステムをBlueMixに移すのはちょっと厳しいけど
新しくサービスを作る立ち上げるのならAWSよりはるかに簡単に扱えます。
で、コンテンツの更新は基本DBやCache等で行い
ファイル更新andデプロイでは基本やらないなどの
ちょっと違った考え方で作りこむのがいいでしょうね。
(気軽にデプロイ出来るとはいえ、設定ミスるとデプロイでコンテナが破棄されたままになるのはちょっと危険・・)
あとは、ログの収集やCronやバッチを動かすのはどうするんだろ?ってところと
デプロイしたアプリが世代管理されていて
いざという時、世代を戻せるとかそういうリカバリ機能があるといいなー


BlueMix、使いこなせたらインフラ管理しなくてもいいから超楽しいかもー



こちらの記事はとても参考にさせていただきました。
ありがとうございました。


まだプログラマーですが何か?
BlueMix 上で PHP アプリを動かす
http://dotnsf.blog.jp/archives/1000985586.html

ソーシャルメディアでのイイね数とか取る方法

facebook

https://api.facebook.com/method/fql.query?query=select%20like_count,%20total_count,%20share_count,%20click_count%20from%20link_stat%20where%20url=%22http://www.yyy.com/zzz/%22

twitter

http://urls.api.twitter.com/1/urls/count.json?url=http://www.yyy.com/zzz/

google+

https://apis.google.com/_/+1/fastbutton?url=http://www.yyy.com/zzz/

内容をパースするらしい
参照
http://hayashikejinan.com/webwork/sns/358/

はてブ

http://api.b.st-hatena.com/entry.count?url=http%3A%2F%2Fwww.yyy.com%2Fzzz%2F

参照
http://developer.hatena.ne.jp/ja/documents/bookmark/apis/getcount

httpd.conの位置、場所一覧

インストールの仕方によってhttpd.confの位置は様々です

例1

/etc/httpd/conf/httpd.conf
OS初期設定やrpmなどのパッケージで入れた場合

例2

/usr/local/apache2/conf/httpd.conf
ソースをconfigure、makeで入れた場合
(自分はこっちが好きです)



端的に言えばapachectrlや/etc/init.d/httpdを見れば
どこにあるかわかります

twitterbotがInvalid auth/bad request (got a 403, expected HTTP/1.1 20X or a redirect)でつぶやかない・・

またつぶやかなくなってた。。ので調査したが
Invalid auth/bad request (got a 403, expected HTTP/1.1 20X or a redirect)
のエラーメッセージでぐぐっても出てこない。。

もしかしてURLが変わったとかかなーって
http://api.twitter.com/1.1/statuses/update.json
で検索したら
どうもhttpsで無いと受け付けなくなった様子。。
http://api.twitter.com/1.1/statuses/update.json

https://api.twitter.com/1.1/statuses/update.json
にしたら動きました。