LoginSignup
2
3

More than 3 years have passed since last update.

laravel guzzle post 通信

Posted at

laravel guzzle http リクエストしようぜ

今回は yahoo の 形態素解析を使ってみる
https://developer.yahoo.co.jp/webapi/jlp/ma/v1/parse.html

まずは composer


composer require guzzlehttp/guzzle

controller で以下のように

今回 twitter の client とかぶってエラーになるので、
use GuzzleHttp\Client2; として 2 をつけて読み込んでおく。


//        use GuzzleHttp\Client2; としておく。
//        他の Client と バッティングさせないために 以下のように書く

$client_id = 'hogeyour';
$secret_id = 'hogesecretyour';

$client = new \GuzzleHttp\Client([
'base_uri' => 'https://jlp.yahooapis.jp/MAService/V1/',
]);

$method = 'POST';//GET or POST
$uri = 'parse?appid='.$client_id.'&results=ma,uniq&uniq_filter=9%7C10&sentence=恋に恋焦がれ恋に泣く';   // スカイツリーの郵便番号
$options = [];
$response = $client->request($method, $uri, $options);

//xml で帰ってくるので。
$data = simplexml_load_string($response->getBody()->getContents());

foreach ($data->uniq_result->word_list->word as $v) {

pd($v->surface);

}

結果


/var/www/html/uranaibako.com/app/Http/Controllers/HogeController.php (line 221)
SimpleXMLElement Object
(
[0] => 恋
)
/var/www/html/uranaibako.com/app/Http/Controllers/HogeController.php (line 221)
SimpleXMLElement Object
(
[0] => 恋焦がれ
)
/var/www/html/uranaibako.com/app/Http/Controllers/HogeController.php (line 221)
SimpleXMLElement Object
(
[0] => 泣く
)

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3