technicalsuwako.moe/www/blog/tadashi-wai-no-api-de-todof.../index.html

112 行
6.8 KiB
HTML
Raw Blame 履歴

このファイルには曖昧(ambiguous)なUnicode文字が含まれています

このファイルには、他の文字と見間違える可能性があるUnicode文字が含まれています。 それが意図的なものと考えられる場合は、この警告を無視して構いません。 それらの文字を表示するにはエスケープボタンを使用します。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja">
<head>
<meta content="text/html; charset=utf-8" http-equiv="content-type" />
<meta name="author" content="テクニカル諏訪子" />
<meta name="description" content="デジタル自主のロリ神様" />
<link rel="alternate" type="application/atom+xml" title="テクニカル諏訪子 feed" href="../../blog.atom" />
<title>テクニカル諏訪子: 【日本API・PHP】簡単に都道府県名等の取り出し方</title>
<link rel="stylesheet" type="text/css" href="../../static/style.css" />
</head>
<body>
<div class="jswarning" id="jswarning">注意JavsScriptはONです。安全・安心の為、JavaScriptをOFFにしてお願い致します。</div>
<script type="text/javascript">
document.getElementById('jswarning').style.display = 'block';
</script>
<div class="container">
<img src="/static/Eqjk_WgVQAE2psn-new.jpeg" class="header-img" alt="ヘッダー" />
<p>
<a href="/">トップ</a> |
<a href="/about/">自己紹介</a> |
<a href="/contact/">連絡先</a> |
<a href="/freelancer/">フリーランス</a> |
<a href="/service/">サービス</a> |
<a href="/webring/">リング</a> |
<a href="/i2pabout/">I2Pについて</a>
</p>
<p>
<a href="/blog.atom"><img src="/static/rssa.png" alt="RSS" />で登録</a> |
<a href="https://social.076.ne.jp/TechnicalSuwako"><img src="/static/mastodon.png" alt="Mastodon" />/<img src="/static/pleroma.png" alt="Pleroma" />でフォロー</a> |
<a href="https://video.076.ne.jp/c/technicalsuwako"><img src="/static/peertube.png" alt="PeerTube" />で登録</a> |
<a href="https://www.technicalsuwako.jp"><img src="/static/clearnet.png" alt="一般ネット" /></a> |
<a href="http://6qiatzlijtqo6giwvuhex5zgg3czzwrq5g6yick3stnn4xekw26zf7qd.onion/"><img src="/static/tor.png" alt="Tor" /></a> |
<a href="http://technicalsuwako.i2p/"><img src="/static/i2p.png" alt="I2P" /></a>
</p>
<p>
支援♡:
<a href="/support/monero/"><img src="/static/monero.png" alt="Monero" /></a> |
<a href="/support/wownero"><img src="/static/wownero.png" alt="Wownero" /></a> |
<a href="/support/bitcoin"><img src="/static/bitcoin.png" alt="Bitcoin" /></a>
</p>
<hr />
<div class="header">
<h1>【日本API・PHP】簡単に都道府県名等の取り出し方</h1>
<a href="/">トップページに戻る</a>
<div class="info">
凜、
2020年02月10日
&mdash;
<a href="/tags/jp/">jp</a>,
<a href="/tags/blog/">blog</a>,
<a href="/tags/php/">php</a>,
<a href="/tags/api/">api</a>
</div>
</div>
<hr />
<div>
<p>日本APIで都道府県情報を取ることが簡単になりますね。<br />
でも、どうしようか?<br />
お説明しましょ〜う!!</p>
<pre style="background-color:#f8f8f8;overflow-x: auto"><code><span style="color:#666">&lt;?</span>php
<span style="color:#a2f;font-weight:bold">function</span> <span style="color:#00a000">getPref</span> (<span style="color:#b8860b">$i</span>) {
ini_set(<span style="color:#b44">&#34;allow_url_fopen&#34;</span>, <span style="color:#666">1</span>);
<span style="color:#b8860b">$json</span> <span style="color:#666">=</span> file_get_contents(<span style="color:#b44">&#39;https://www.technicalsuwako.jp/api/rpc/japan/geo/prefecture&#39;</span>);
<span style="color:#b8860b">$obj</span> <span style="color:#666">=</span> json_decode(<span style="color:#b8860b">$json</span>);
<span style="color:#a2f;font-weight:bold">return</span> <span style="color:#b8860b">$obj</span>[<span style="color:#b8860b">$i</span>];
}
</code></pre><p>それでURLから都道府県情報を取り出しますと、arrayに変換します。<br />
機能性の使い方法は:</p>
<pre style="background-color:#f8f8f8;overflow-x: auto"><code> <span style="color:#a2f;font-weight:bold">echo</span> getPref(<span style="color:#666">13</span>)<span style="color:#666">0</span><span style="color:#666">-&gt;</span><span style="color:#b44">pref</span><span style="color:#666">-&gt;</span><span style="color:#b44">name</span>;
<span style="color:#a2f;font-weight:bold">echo</span> <span style="color:#b44">&#39;&lt;br &gt;&#39;</span>;
<span style="color:#a2f;font-weight:bold">echo</span> getPref(<span style="color:#666">1</span>)<span style="color:#666">-&gt;</span><span style="color:#b44">pref</span><span style="color:#666">-&gt;</span><span style="color:#b44">name</span>;
<span style="color:#a2f;font-weight:bold">echo</span> <span style="color:#b44">&#39;&lt;br /&gt;&#39;</span>;
<span style="color:#a2f;font-weight:bold">echo</span> getPref(<span style="color:#666">27</span>)<span style="color:#666">-&gt;</span><span style="color:#b44">pref</span><span style="color:#666">-&gt;</span><span style="color:#b44">name</span>;
<span style="color:#a2f;font-weight:bold">echo</span> <span style="color:#b44">&#39;&lt;br /&gt;&#39;</span>;
<span style="color:#a2f;font-weight:bold">echo</span> getPref(<span style="color:#666">47</span>)<span style="color:#666">-&gt;</span><span style="color:#b44">pref</span><span style="color:#666">-&gt;</span><span style="color:#b44">name</span>;
</code></pre><p>下記のものが書き出します。</p>
<pre style="background-color:#f8f8f8;overflow-x: auto"><code> 東京都
北海道
大阪府
沖縄県
</code></pre><p>同じく、「<code>-&gt;nameKana</code>」で平仮名が書き出すし、「<code>-&gt;nameEng</code>」で英語名が書き出す等。<br />
ファイルを読んだら、確かに全ての可能性をわかるとなります。<br />
<a href="https://www.technicalsuwako.jp/api/rpc/japan/geo/prefecture">都道府県API</a></p>
<p>以上</p>
</div>
<hr />
<a href="https://github.com/piranha/gostatic">gostatic</a>で創作しました。<a href="https://git.076.ne.jp/TechnicalSuwako/StaticSuwako"><img src="/static/git.png" alt="Git" /></a>
<a href="http://validator.w3.org/check?uri=https%3A%2F%2Fwww.technicalsuwako.jp%2F">
<img src="http://www.w3.org/Icons/valid-xhtml11" alt="Valid XHTML 1.1" height="31" width="88" />
</a>
<a href="https://jigsaw.w3.org/css-validator/validator?lang=ja&amp;profile=none&amp;uri=https%3A%2F%2Fwww.technicalsuwako.jp%2F&amp;usermedium=all&amp;vextwarning=&amp;warning=1">
<img style="border:0;width:88px;height:31px" src="https://jigsaw.w3.org/css-validator/images/vcss" alt="正当なCSSです!" />
</a>
<a href="https://anybrowser.org/campaign/images/button_enhanced.gif">
<img style="border:0;width:88px;height:31px" src="https://anybrowser.org/campaign/bvgraphics/any1.gif" alt="どっちでもブラウザで表示出来ます!" />
</a>
</div>
</body>
</html>