@blog.justoneplanet.info

日々勉強

Yahooひらがな変換API

ドキュメントの文字コードはutf-8である。

<?php
$str = urlencode('庭には二羽鶏がいる。');
$url = "http://jlp.yahooapis.jp/FuriganaService/V1/furigana?appid=<apikey>&sentence={$str}";
$xml = new SimpleXmlElement($url, null, true);
var_dump($xml);
/*
object(SimpleXMLElement)#1 (1) {
  ["Result"]=>
  object(SimpleXMLElement)#2 (1) {
    ["WordList"]=>
    object(SimpleXMLElement)#3 (1) {
      ["Word"]=>
      array(8) {
        [0]=>
        object(SimpleXMLElement)#4 (3) {
          ["Surface"]=>
          string(3) "庭"
          ["Furigana"]=>
          string(6) "にわ"
          ["Roman"]=>
          string(4) "niwa"
        }
        [1]=>
        object(SimpleXMLElement)#5 (3) {
          ["Surface"]=>
          string(3) "に"
          ["Furigana"]=>
          string(3) "に"
          ["Roman"]=>
          string(2) "ni"
        }
        [2]=>
        object(SimpleXMLElement)#6 (3) {
          ["Surface"]=>
          string(3) "は"
          ["Furigana"]=>
          string(3) "は"
          ["Roman"]=>
          string(2) "ha"
        }
        [3]=>
        object(SimpleXMLElement)#7 (3) {
          ["Surface"]=>
          string(6) "二羽"
          ["Furigana"]=>
          string(6) "にわ"
          ["Roman"]=>
          string(4) "niwa"
        }
        [4]=>
        object(SimpleXMLElement)#8 (3) {
          ["Surface"]=>
          string(3) "鶏"
          ["Furigana"]=>
          string(12) "にわとり"
          ["Roman"]=>
          string(8) "niwatori"
        }
        [5]=>
        object(SimpleXMLElement)#9 (3) {
          ["Surface"]=>
          string(3) "が"
          ["Furigana"]=>
          string(3) "が"
          ["Roman"]=>
          string(2) "ga"
        }
        [6]=>
        object(SimpleXMLElement)#10 (3) {
          ["Surface"]=>
          string(6) "いる"
          ["Furigana"]=>
          string(6) "いる"
          ["Roman"]=>
          string(3) "iru"
        }
        [7]=>
        object(SimpleXMLElement)#11 (1) {
          ["Surface"]=>
          string(3) "。"
        }
      }
    }
  }
}
*/
foreach($xml->Result->WordList->Word as $word){
    print($word->Furigana);
}
//にわにはにわにわとりがいる

文字コードがshift-jisの場合は以下のようになる。

$str = urlencode(mb_convert_encoding('庭には二羽鶏がいる。', 'utf-8', 'sjis'));
$url = "http://jlp.yahooapis.jp/FuriganaService/V1/furigana?appid=<apikey>&sentence={$str}";
$xml = new SimpleXmlElement($url, null, true);
foreach($xml->Result->WordList->Word as $word){
    print($word->Furigana);
}

API

コメントはまだありません»

No comments yet.

RSS feed for comments on this post.TrackBack URL

Leave a comment