TRY AND ERROR

気になったこと、勉強したこと、その他雑記など色々メモしていきます。。Sometimes these posts will be written in English.,

ChatworkAPIがバージョンアップしました。

ChatworkAPIがv1からv2にバージョンアップしました。

help.chatwork.com


ユーザー数が増えてmessage_idの桁が足りなくなったみたいな感じか??
対応としては、基本エンドポイントを変更するくらいです。

以前アップしたChatworkのAPIラッパーについてはは修正済みです。

github.com
github.com
github.com

How to check if index is exist in Array or Slice in Golang?

I guess in Golang, there isn't any method to check that index is exist in Slice, for PHP like "isset()".
So I take a method I made like below.

func isset(arr []string, index int) bool {
    return (len(arr) > index)
}

By the way, in Golang when you check that index is exist in array and then also check that value of index of array is not blank"", it works like below.

// Assume "len(arr) == 2"  in this time
if (isset(arr, 3) && arr[3] != "") {
    // Any code.
}

Though the right side divided with "&&" looks likely to throw the error "out of range", the left side is false that's why the right side is ignored in Golang.


You have any ideas about "isset()"??

In CakePHP3, change just a specific table class's datasource.

If you want to get the Table objects to access to DB without appalently using ConnectionManager, I think you use the "TableRegistry" like following.

TableRegistry::get("Yours");


Above then, "default" datasource in app.php is chosen.
If you use the datasource of not default, you set "defaultConnectionName" function to Model/Table/YoursTable.php

public static function defaultConnectionName() {
    return 'your datasource key';   // Defined at Datasources in app.php.
}

Just only it!

google-api-php-clientを使ったOAuth2認証で、セッションを使わずにコールバックを経由しても保持される任意のパラメータをセットする

OAuth2認証において、

Googleに認証リクエスト → Google認証画面にてアプリ許可 → コールバック画面に遷移

という流れの中で、コールバックをもらった際にどのユーザー(アプリ側の)で認証したかを持ち回る必要があった。


アプリ側で必要となる任意のパラメータをコールバック時にGoogleから返してもらいたいときは、
認証urlのリクエストパラメータ「state」をセットする。

$client = new Google_Client();

// 中略....

$params = ['usr_id' => 'xxxx'];
$client->setState(json_encode($params));
$client->authRequest();

こうすると、認証時に指定した任意のパラメータをコールバック画面で受け取れる。

$usr_id = json_decode($_GET["state"],true)["usr_id"] ?? "";

In the HTML5, "readonly" attribute just works for text fields.

In the HTML5, "readonly" attribute just works for text fields like <input type="text"> or <textarea>...
I didn't know about that!

For example, in the case of to put an select tag.

Like this, it doesn't work as you think. It is editable.

<select readonly>
    <option value="v1">v1</option>
    <option value="v2">v2</option>
    <option value="v3">v3</option>
</select>


If you set a "disabled" attribute Instead of "readonly", It seems to be correct but you cannot receive this in server side when you submitted. So it doesn't work too.

<select disabled>
    <option value="v1">v1</option>
    <option selected value="v2">v2</option>
    <option value="v3">v3</option>
</select>


For result, If you want to make a not editable "select" tag, you should set a "disabled" attribute to child items but the selected one.
This works as not editable and could receive in server side.

<select>
    <option disabled value="v1">v1</option>
    <option selected value="v2">v2</option>
    <option disabled value="v3">v3</option>
</select>


Hmm...I feel It is a little technically.

Cakephp3のShellがcronで動かなかった理由

デイリーの集計絡みのバッチを書いていてちょいはまったのでメモ。


まず、 CakeのShellをcronで叩く際のOfficialの記載はこう

15 0 * * * cd /full/path/to/app && bin/cake myshell myparam

これが動かなかった。


次に試したのは、そもそもcdする必要ないんじゃないかということで
こんな感じでやった。

15 0 * * * /full/path/to/app/bin/cake myshell myparam

しかしこれもだめ。



cron通さずに普通に実行してみると、これは動いた!

 $ /full/path/to/app/bin/cake myshell myparam



ログを出してみると、cronで叩いたときのphpのバージョンが違っていたので、
今回はcakeの問題ではなかった。。

PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/mecab.so' - /usr/lib64/php/modules/mecab.so: cannot open shared object file: No such file or directory in Unknown on line 0
PHP Fatal error:  You must enable the intl extension to use CakePHP. in 


バイナリではなくbin/cake.phpphpのバージョンを指定して叩くことで対応しました。

15 0 * * * /usr/bin/php70 /full/path/to/app/bin/cake.php myshell myparam

phpQueryでタグの破片などのノイズを除きつつテキストっぽいものだけを取得する。

とりあえず行き着いた方法をメモします。
htmlによっては、先頭にxmlステートメントが混ざってくる場合が稀にあるので、最初に除去しておく。
さらに、なぜかscriptやstyleの破片などがノイズとして紛れ込むケースが多々あるので、これらも除去。

newDocumentする前の下ごしらえが結構重要なので、この辺のチューニングが鍵になります。

// Remove xml statement.
$html = preg_replace('/^<\?xml.*\?>/', '', $html);
// Remove noises.
$html = preg_replace(array("/\r/","/\n/","/\r\n/","/<br \/>/","/<br>/","/<script.*?\/script>/","<style.*?\/style>","/ /"),"",$html);

$doc = phpQuery::newDocument($html);
$title = $doc["title"]->text();
$body = $doc['body']->text();