[API]phpでAPIにアクセスしてgetJSON()で取得する。APIキーを隠蔽する方法。

カテゴリー:
jQuery

以前の投稿でぐるなびAPIを利用したSPAのサンプルをご紹介しましたが、JavaScriptで作ったため、本来公開すべきではないAPIキーを隠蔽できない、という問題がありました。

それを解決する方法として、PHPでAPIキーを使って情報を取得し、それをJavaScript側で取得する、という方法があるとわかったのでサンプルコードを書いてみました。

実際の動作は以下のdemoから確認できます。

サンプルコード

demoのコードを簡略化したものを以下にご紹介します。

html

ボタンをクリックするとイベントが発生し、.resultの中にAPIから取得したデータを表示させます。

PHP

APIのURLにパラメーターを付けてアクセスするとAPIからデータが戻ってくるのですが、(ぐるなびAPIの場合)必須のパラメーターのひとつにAPIキーがあります。

つまり、JavaScriptのイベントなどで非同期にAPIにアクセスする場合、jQueryを使うならajax()やgetJSON()などを使ってAPIキーを含んだパラメーター付きのURLにアクセスする必要があるのですが、jsファイルなどにAPIキーを埋め込むと当然隠蔽できません。

なので、phpなどでサーバー側でAPIにアクセスして処理結果を得られる仕様にすれば、APIキーが隠蔽できる、ということになります。

というわけで以下のphpでAPIを叩いてデータを取得。JavaScriptに戻す値を生成します。

JavaScript(jQuery)

getJSON()の戻り値として、(result)の中にproxy.phpで生成されたJSONデータが入っています。それを使ってhtmlを生成します。

まとめ

非同期通信の勉強を始めたばかりで、この投稿でご紹介したphpと連携するやり方も今まで知りませんでした。

慣れた方にとっては標準的なやり方なのだろうと思いますが、これはなかなかおもしろいですね。非同期通信関連の処理は他にもいろいろと面白い機能があるようで、今後も楽しみに勉強を続けて行きたいと思います!

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

コメント

For most up-to-date information you have to visit internet and
on internet I found this site as a most excellent site for latest updates.

2017/07/1711:44 | situs judi bola resmi

The city also offers various recreational areas and kids would enjoy a call
to any kind of them. Latin America’s prominent role inside the
non-aligned movement through the Cold War and widespread popular opposition to the giant
northern neighbor helped influence the creation of Tercer Cine, Third Cinema, as a backlash against Hollywood, US cultural dominance and capitalism.

Bald Mountain attracts experienced skiers from all over the world, as well like a decent share of celebrity and
well-to-do travelers as well.

2017/09/0706:03 | travel sites that accept bitcoin

What you need to have is willpower to become taught the way
to create income generating blogs and make your
organization stable. If one is careful to not offend,
he then or she could generate a large number of new readers.
This is a gesture of proper will confident that other bloggers
can do the identical.

2017/09/1117:22 | latest updates pokemon go

Once all of that is at place you will be ready to start
earning from a blog. Use articles – The best way you
may get your company to rate high on a search engine is to focus on the right keywords that men and women are choosing to find a law firm.
Writing blog posts may be a way to channel knowing about it and creativity with the world.

2017/09/1522:50 | blogilates cardio

But all blogs will have one thing in keeping, regardless if you are blogging for riches as
well as to get attention. Many individuals who are looking
for ways to lower your expenses have discovered out precisely how easy it really
is once you buy generic. Parents could get the animated or children’s movies at will, as well as the kids can be quiet for a
few hours.

2017/09/1522:53 | iblog jones

What you need to have is willpower being taught the way to create
profitable blogs and your company stable. There are many blog networks around that allow you to submit you and promote your blog.
The informal design of a blog gives Internet consumers a chance to become familiar
with individuals who’re behind the organization better, that
makes it easier to enable them to trust you together with spend money on your site.

2017/09/1523:06 | education blog directory

However, after one donation, you’ll be able to donate again only after 3 months once your body has
regained the donated blood. Then write the way looks like for you and
pay attention when you lay them in the market to learn more about
yourself and watch how we will see the hidden lesson. Instead of getting the problem taken care of, I self-medicated
and allowed it to get worse.

2017/09/1523:23 | world news vietnam

” Not being a philosopher or even a physicist, I’m not planning to learn about that subject in publications, but I can advise you that, from your marketing standpoint, the web marketing effect on the business mentioned previously was obviously a big fat zero. An eyewitness account from the Independent’s Andrew Buncombe, who was shot while taking refuge with 1,500 ‘red shirt’ protesters inside a temple – a supposed safe home – received jibes to inside pay of Thaksin. News keeps us talking to political affairs not just of our country but additionally of on the world.

2017/09/1523:58 | cbc news internet tv

So recommendations too few to convince you that
you’d be doing who you are a disservice by wanting to source your own personal finance – well – you happen to be special.
Today’s world does not require all types of business to start out its first day in operations away from a
10-story workplace with leather couches plus a glitzy waiting room.
If I receive comments in respond to this article towards the effect that life isn’t that fair, this
perception is, in itself a useless and self-defeating thought whose energy is creating ongoing lack.

2017/09/1523:58 | https://Givesunlight.com/

scroll as a result of where it says HKEY_CURRENT_USER and expand it by pressing the plus sign alongside it.
There is no need of calling in an experienced to help increase computer performance as you
possibly can done easily by any individual. This kind of
breakthrough is the thing that many musicians and artists alike have
been looking forward to in lots of years already.

2017/09/1603:13 | computer disposal

And live inside arms of this fictitious Adonis which you have conjured since your fantasy
mate. It’s the same voice suggesting which you shouldn’t have inked it when you ignored Him the
first time. The individual who I quoted at the beginning of these tips have
actually done a good job over the last year and a half, using
social media to build a substantial and responsive list for himself.

2017/09/1603:19 | giveuselife.org

Where to buy cheap nfl jerseys? Come here. discount jerseys nfl

2017/09/1922:08 | discount jerseys nfl

And, think it over: by refusing small things
in your life, for instance a compliment or someone holding the doorway in your case whenever your arms are filled with
packages, actually can make it harder to receive even larger gifts that will present themselves.
The difficulties with the economic conditions nowadays make us want more savings and less out-of-pocket spending.
Many lenders have become coming for the negotiation table of short
sale a little easier now this might be because of being overwhelmed with foreclosures plus much more foreclosures on the horizon.

2017/09/2210:35 | finance 6

The Nokia X3 Red Mobile has a MP3, MP4, e-AAC+, WMA audio and video files are easily playable
for the cell. You feel if you had given your relationship
more effort and time it will been employed out.
Skinny Jeans – This piece will usually remain stylish
since its flatters all body shapes and sizes.

2017/09/2210:48 | social news from london zoo

However, after one donation, you can donate again only after a few months
whenever your body has regained the donated blood. Then write the actual way it looks like to you and pay attention when you lay
the cards to find out more on yourself and view the method that you will find the hidden lesson. Over the arrival months I’ll be
writing a few articles revealing the techniques to advertising (all the things a journalist won’t want you to know.

2017/09/2211:33 | breaking news eenadu

The phone is integrated with 8-GB and 16-GB internal memory and expandable Micro – SD storage device option that supports
around 32GB memory. An eyewitness account through the Independent’s
Andrew Buncombe, who was simply shot while taking refuge with 1,500
‘red shirt’ protesters in a temple – a supposed safe place – received jibes being inside pay of Thaksin. Skinny Jeans – This piece will forever remain stylish since its flatters all body shapes and sizes.

2017/09/2211:51 | news 4 june 2017

Here are five nifty and easy steps to produce that clinching first info product.
During breeding, provide ample food and be sure that there could be less competition in the aquarium tank.

A simple call for the local City Hall Offices or County Officials Buildings should support these questions.

2017/09/2213:51 | software 7.1

What you need to have is willpower to be taught the way to create wealth creation blogs and make your business stable.
There are many blog networks around where you can submit you and promote your blog.

The informal style of your blog post gives Internet consumers
a chance to become familiar with individuals who’re behind the business
enterprise better, which makes it easier to enable them to trust you
together with buy your site.

2017/09/2219:14 | business 83 auto sales

You will get to check out honest review about The Chan Do Internet
Success System. It is very important in the case of organic SEO campaign where results trickle gradually on the any period of time of
time. The conversation from the customer satisfaction employee and a customer should be in the clean way.

2017/09/2219:29 | https://atticusblog.com