エラーメッセージをじっくり読んだらわかるかな。
$ mecab -h 〜 省略 〜 -b, --input-buffer-size=INT set input buffer size (default 8192) 〜 省略 〜
どうやら、バッファーが足りないらしいお。(^ω^
しかし
- test.php
<?php // mecabのパス $path = '/usr/local/bin/mecab'; /* 標準入力を利用し、キーボードからの入力を受け付ける */ $stdin = fopen( 'php://stdin' , 'r' ); $str = fgets( $stdin ); fclose( $stdin ); if (!function_exists('stream_get_contents')) { function stream_get_contents($handle) { $contents = ''; while (!feof($handle)) { $contents .= fread($handle, 8192); } return $contents; } } $descriptorspec = array( 0 => array("pipe", "r") , 1 => array("pipe", "w") ); $result = ""; $process = proc_open($path, $descriptorspec, $pipes); if (is_resource($process)) { fwrite($pipes[0], $str); fclose($pipes[0]); $result = stream_get_contents($pipes[1]); fclose($pipes[1]); proc_close($process); } echo $result;
実行してみるけど、ダメでした。
$ php test.php
(´・ω・`)?
え。文字列「テスト」だぜ。バイトにしてもせいぜい12byteぐらいでしょ?
何故だ・・・? と、探しつつ、オプションを付与してあげてもよくわかりません。
よくわかんねぇので、エラーを出力しないように箱を用意してあげます。
- test_2.php
<?php // mecabのパス $path = '/usr/local/bin/mecab'; /* 標準入力を利用し、キーボードからの入力を受け付ける */ $stdin = fopen( 'php://stdin' , 'r' ); $str = fgets( $stdin ); fclose( $stdin ); if (!function_exists('stream_get_contents')) { function stream_get_contents($handle) { $contents = ''; while (!feof($handle)) { $contents .= fread($handle, 8192); } return $contents; } } $descriptorspec = array( 0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w") // この番号はlinuxでいうファイルディスクリプタの数値なので、エラー出力を取得 ); $result = ""; $error = ""; $process = proc_open($path, $descriptorspec, $pipes); if (is_resource($process)) { fwrite($pipes[0], $str); fclose($pipes[0]); $result = stream_get_contents($pipes[1]); fclose($pipes[1]); fwrite($pipes[2], $error); fclose($pipes[2]); proc_close($process); } echo $result; echo $error;
これで大丈夫。
・・・と、思ったんだけど。実行してみると、なぜか 「 echo $error; 」が出力されない。
なんでだ? (ごめん、これだけが未解決)