0
3

More than 3 years have passed since last update.

ぐるなびAPIと占いAPIに触れてみた。

Last updated at Posted at 2020-07-25

今久しぶりにPHPを触っていて、APIが動いて感動したので書き留めます。
初歩的な内容です。。

ぐるなびPHP

拾い物のPHPをちょっと修正して
ぐるなびAPIコードは登録すれば無料で使えます。

xamppでapacheを起動させて、
仮のサーバーフォルダ?の中に入れて、ブラウザでlocalhost/php/guru.phpに移動すると動きます。

guru.php

// 定義
define("GURUNAVIAPI_ACCESS_KEY", "APIコードを入れてください。");
define("INTERNAL_ENC", "UTF-8");
define("ARR_CONTEXT_OPTIONS", null);

// START
function gurunavi_search_restlist_v3($hit_per_page=30, $offset_page=1, $freeword) {
    $ret = FALSE;

    $search_url = "https://api.gnavi.co.jp/RestSearchAPI/v3/?"
    . "keyid=" . GURUNAVIAPI_ACCESS_KEY
    . "&hit_per_page=" . $hit_per_page
    . "&offset_page=" . $offset_page
    . "&freeword=" . urlencode_rfc3986(mb_convert_encoding($freeword, "UTF-8"));

    $json = json_decode(mb_convert_encoding(@file_get_contents($search_url, false, ARR_CONTEXT_OPTIONS), "UTF-8", INTERNAL_ENC));

    if ($json !== null) {
    $ret = array();
    $n = 0;
    foreach ($json->rest as $shop) {
    // 中略
    $ret[$n]['id'] = (string)$shop->id;
    $ret[$n]['name'] = (string)$shop->name;
    $ret[$n]['address'] = (string)$shop->address;

    $ret[$n]['shop_image1'] = (string)$shop->image_url->shop_image1;
    $ret[$n]['url'] = (string)$shop->url;
    $ret[$n]['line'] = (string)$shop->access->line;
    $ret[$n]['station'] = (string)$shop->access->station;
    $ret[$n]['station_exit'] = (string)$shop->access->station_exit;
    $ret[$n]['walk'] = (string)$shop->access->walk;

    // 中略
    $n++;
    }
    if (count($ret) <= 0) {
    $ret = FALSE;
    }
    }
    return($ret);
    }
    // END

//中略
 $rests = gurunavi_search_restlist_v3(30, 1, "味噌汁");

 foreach ($rests as $rest) {
  echo('<h2>' . $rest['name'] . '</h2>');
  echo('<center>');
  echo('<img src="' . $rest['shop_image1'] . '" alt="' . $rest['name'] . '" width="192">');
  echo('</center>');
  echo('<br />');
  echo('<b><a href="' . $rest['url'] . '" target="_blank" rel="nofollow noopener">' . $rest['name'] . '</a></b>
');
  echo("<b>住所</b> " . $rest['address'] . "<br />");
  echo("<b>アクセス</b> " . $rest['line'] . ' ' . $rest['station'] . ' ' . $rest['station_exit'] . " 徒歩" .  $rest['walk'] . "分 " . "<br />");
 }

 //文字をエンコードするための関数
function urlencode_rfc3986($str)
{
return str_replace('%7E', '~', rawurlencode($str));
}

APIはURLにパラメーターをつけて送れば、答えが届きます。
その答えを一つずつほぐして、表示させます。

デバッグ

vscodeでxdebugでデバッグをすると意味がよくわかります。

デバッグ画像
json.jpg

jsonの中にある、rest達をshopに入れる

guru.php
$json->rest as $shop

それを一つずつほぐす

guru.php
foreach($json->rest as $shop)

用意した配列に、shopの内容を入れていく。
配列に shopのidを入れる。

guru.php
 if ($json !== null) {
    $ret = array();
    $n = 0;
    foreach ($json->rest as $shop) {
    // 中略
    $ret[$n]['id'] = (string)$shop->id;

retの配列を中身を見ると、確かに入ってる。
json.jpg

自作関数は、ぐるなびの結果を、retの配列を返り値として返します。
その配列を、一つずつほぐして今後は出力

guru.php
function gurunavi_search_restlist_v3($hit_per_page=30, $offset_page=1, $freeword) {
    }
// 中略
    return($ret);
    }

$rests = gurunavi_search_restlist_v3(30, 1, "味噌汁");

 foreach ($rests as $rest) {
  echo('<h2>' . $rest['name'] . '</h2>');
  echo('<center>');
  echo('<img src="' . $rest['shop_image1'] . '" alt="' . $rest['name'] . '" width="192">');
  echo('</center>');
  echo('<br />');
  echo('<b><a href="' . $rest['url'] . '" target="_blank" rel="nofollow noopener">' .

出力結果
フリーワードで「味噌汁」で引っかかるお店一覧が出てきました。感動。
miso.jpg

あとがき
コードを修正して、占いAPIも作ってみました。

uranai.php
<?php 
// START

define("INTERNAL_ENC", "UTF-8");
define("ARR_CONTEXT_OPTIONS", null);

//http://api.jugemkey.jp/api/horoscope/free/2020/07/01

// START
function today_fortune($today) {

    $ret = FALSE;

    $search_url = "http://api.jugemkey.jp/api/horoscope/free/".$today;
        $json = json_decode(mb_convert_encoding(@file_get_contents($search_url, false, ARR_CONTEXT_OPTIONS), "UTF-8", INTERNAL_ENC));

    if ($json !== null) {
    $ret = array();
    $n = 0;

    foreach ($json->horoscope->$today as $result) {
    // 中略
    $ret[$n]['content'] = (string)$result->content;
    $ret[$n]['item'] = (string)$result->item;
    $ret[$n]['money'] = (string)$result->money;

    $ret[$n]['total'] = (string)$result->total;
    $ret[$n]['job'] = (string)$result->job;
    $ret[$n]['color'] = (string)$result->color;
    $ret[$n]['love'] = (string)$result->love;
    $ret[$n]['rank'] = (string)$result->rank;
    $ret[$n]['sign'] = (string)$result->sign;

    // 中略
    $n++;
    }
    if (count($ret) <= 0) {
    $ret = FALSE;
    }
    }
    return($ret);
    }
    // END

//中略
$today = date('Y/m/d');
 $rests = today_fortune($today);

 echo '今日の'.$today.'の運勢';
 foreach ($rests as $rest) {

  echo('<center>');  
  echo('<h2>' . $rest['sign'] . '</h2>');
  echo('<p>ランキング' . $rest['rank'] .'</p>');
  echo('<p>総合点' . $rest['total'] .'</p>');
  echo('<p>金運' . $rest['money'] .'</p>');
  echo('<p>仕事運' . $rest['job'] .'</p>');
  echo('<p>恋愛運' . $rest['love'] .'</p>');
  echo('<p>コメント' . $rest['content'] .'</p>');
  echo('<p>ラッキーアイテム' . $rest['item'] .'</p>');
   }
?>

powerd by JugemKey
【PR】原宿占い館 塔里木

体裁は適当ですが、キレイにしたら占いコンテンツの出来上がりです。

0
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
0
3