TwitterのTL・フォローユーザとInstagram(インスタグラム)でつながる方法:PHPでInstagram APIの使い方など
TwitterのTL・フォローユーザとInstagram(インスタグラム)でつながる方法:PHPでInstagram APIの使い方など
posted by reent on October 27, 2013
こんにちは。皆さん、Instagramはやってますか。
最近、Twitterをはじめたんですが、やっぱInstagramやってる人はそれなりにいると思いますね(特に女性)。

私もInstagramアプリ入れてみました。
http://instagram.com/reenim

InstagramはTwitterからの導線を最初から考えていたことがあり、他の人とのつながりとかコミュニケーション機能があまり充実していないですね。
ただ、APIは用意されているので、これでがんばってつなげてみました(需要あるかな??)。

昔は簡単にTwitterと連携できたみたいなんですが。。
http://jp.techcrunch.com/2012/07/27/20120726no-api-for-you-twitter-shuts-off-find-friends-feature-for-instagram/



APIの資料。
http://instagram.com/developer/#
instagramAPI公開!OAuth触りました
カヤックさん。
RELATIONSHIPエンドポイント

簡単な流れとしては、
①twitter上のTLから"instagram.com"のURLをとってくる(twitterは短縮URL化されてしまいますが、今:2013/10/27現在のところこれとれます)。
②「①」にwgetとかで通信させて、instagramからのレスポンスHTMLをパースして、InstagramのユーザIDを取得。
③Instagram APIを使って、「②」のユーザをInstagram上でフォロー。

これでtwitterで気になってるフォローの写真をInstagamでもチェックできちゃいます(できます)ー(゚ー゚)y-。

で、実際のやり方ですが、APIの利用制限にひっかからない範囲で定期的に実行してフォローできるようになることを想定して、ソースコードを載せながら説明してみます。


◆Instagram APIの使い方
基本的にはカヤックさんの記事通りなので、access_tokenさえあればバッチ処理で常にとってくるみたいなことができます。
軽く調べた感じでは、Instagramのaccess_tokenの有効期限は不明で消えないことになっているみたいです(もちろん規約的に消える可能性は示唆されているので、Instagram側の都合によってある日突然変わるかもしれない)。

access_tokenの取得は、こんな感じ。
* Instagramへデベロッパー登録。
* Instagram上でアプリの登録(このときにアプリURLとリダイレクトURLを指定)。
※Instagram側の想定としては、アプリURLからInstagramの認証URLにリダイレクトさせる。
* Instagramからユーザに対して、アプリの許可設定(ユーザ情報、フレンド情報など)を聞くので、ユーザがOKとすればaccess_tokenを取得するためのコードつきでリダイレクトURLにとばされる(とばしてくれる)。

つまり、codeとaccess_tokenはユーザ個別に発行されるから、バッチ処理的には自分用にしかしづらい><。
ちなみに取得したcodeを2回目に実行されるともう有効じゃないとおこられる(新規に利用可能なcodeを取得しようとすれば、都度ブラウザからのアクセス・認証が必要)。
※参考
InstagramのAPIを使ってみる #2

access_tokenが取得できればあとは割りと簡単。公式のドキュメント通りAPIを実行してみればOK。

・フォローとフォロワーの取得
function get_im_follows()
{
・・・
        $access_token = $arr['access_token'];
        $user_id = $arr['user_id'];

$url = 'https://api.instagram.com/v1/users/'.$user_id.'/follows';
        $url .= "?access_token=".$access_token;

$ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$json = json_decode(curl_exec($ch));
        curl_close($ch);

$data = $json->data;
        return $data;
}

function get_im_followers()
{
・・・
        $access_token = $arr['access_token'];
        $user_id = $arr['user_id'];

$url = 'https://api.instagram.com/v1/users/'.$user_id.'/followed-by';
        $url .= "?access_token=".$access_token;

$ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$json = json_decode(curl_exec($ch));
        curl_close($ch);

$data = $json->data;
        return $data;
}

$arr = get_im_follows();
if (count($arr)) {
print "====follows(".count($arr).")====\n";
foreach($arr as $val) {
        var_dump($val);
}
print "====follows print end====\n";
}
if (count($arr)) {
print "====followers(".count($arr).")====\n";
$arr = get_im_followers();
foreach($arr as $val) {
        var_dump($val);
}
print "====followers print end====\n";
}


ちょっと長くなってしまたったので、twitter連動部分は続きの記事に。。
mixiチェック
このエントリーをはてなブックマークに追加

関連エントリー:

back | top | next

Leave a Comment

Fields with * are required.

本当はスパムですか?
いいえすぱむはい

30 comments

#3624
August 1, 2014 at 02:33 am
Thanks for your personal marvelous posting!
I quite enjoyed reading it, you may be a great author.I will make certain to bookmark
your blog and will eventually come back in the foreseeable future.
I want to encourage you to ultimately continue your great writing,
have a nice afternoon!
#3623
August 1, 2014 at 02:22 am
Hello, I enjoy reading all of your post. I
wanted to write a little comment to support you.
#3621
August 1, 2014 at 01:53 am
Hello, I enjoy reading all of your post. I
wanted to write a little comment to support you.
#3538
Lemuel says:
July 17, 2014 at 07:13 am
Definitely consider that that you said. Your favorite justification seemed to be at the web the
simplest thing to be mindful of. I say to you, I certainly get irked whilst people consider concerns that they plainly do not know about.
You controlled to hit the nail upon the highest and also
outlined out the whole thing without having side-effects , other
people could take a signal. Will probably be back to get more.
Thanks
#3513
Shiela says:
July 16, 2014 at 04:48 am
Nice post. I learn something new and challenging on websites I stumbleupon everyday.
It's always useful to read content from other authors
and use something from their web sites.
#3421
testocore says:
July 7, 2014 at 08:34 pm
This design is steller! You obviously know how to keep
a reader amused. Between your wit and your videos,
I was almost moved to start my own blog (well, almost...HaHa!) Excellent
job. I really loved what you had to say, and more than that, how you presented it.
Too cool!
#3365
July 7, 2014 at 06:53 am
What i don't realize is in reality how you are no longer
really much more smartly-appreciated than you may be right
now. You are very intelligent. You already know thus significantly when it comes to this
matter, made me in my opinion believe it from numerous numerous
angles. Its like men and women don't seem to be interested unless it's one thing to do with Lady gaga!
Your personal stuffs outstanding. At all times maintain it up!
#3327
July 6, 2014 at 08:38 pm
There's definately a great deal to find out about this issue.

I really like all of the points you made.
#3153
July 4, 2014 at 10:36 pm
Great post. I'm going through many of these issues as well..
#3124
July 4, 2014 at 06:57 pm
It is in reality a great and useful piece of information. I am
happy that you shared this helpful info with us. Please
stay us informed like this. Thanks for sharing.
#3006
July 3, 2014 at 05:59 pm
I like the valuable information you provide in your articles.

I will bookmark your weblog and check again here frequently.
I am quite certain I will learn a lot of new stuff right here!

Best of luck for the next!
#2977
safer colon says:
July 3, 2014 at 12:37 pm
Hello, i think that i saw you visited my website thus
i got here to return the prefer?.I am attempting to to find issues
to enhance my website!I suppose its ok to make use of some of
your ideas!!
#2472
June 27, 2014 at 11:31 pm
What's up to all, how is the whole thing, I think every one is getting more
from this site, and your views are good in support of new users.
#2467
June 27, 2014 at 10:05 pm
Another downside of dummy video cameras is they can be discovered.

Thankfully, those old times of simple black and white surveillance
cameras that could only provide us with shady characters and inadequate data feed on limited VHS tapes,
are over today. Be aware that the weather will influence your pictures.
#2363
June 27, 2014 at 03:12 am
After I originally left a comment I appear to have clicked on the -Notify me when new comments are added- checkbox
and now each time a comment is added I receive four emails
with the same comment. There has to be a means you can remove me from
that service? Kudos!
#2348
June 27, 2014 at 12:58 am
I'm not sure why but this weblog is loading extremely slow for me.
Is anyone else having this problem or is it a issue on my end?
I'll check back later and see if the problem still exists.
#2335
June 26, 2014 at 09:57 pm
Excellent site. A lot of helpful info here. I'm sending it to some
pals ans additionally sharing in delicious. And of course, thank you in your effort!
#2306
June 26, 2014 at 11:39 am
Excellent beat ! I wish to apprentice while you amend your web site, how can i subscribe
for a blog website? The account helped me a acceptable deal.

I had been tiny bit acquainted of this your broadcast
offered bright clear idea
#2149
June 25, 2014 at 04:38 am
What must also be considered is the quality of the product.
She is usually depicted as hotheaded and impulsive, especially when dealing with something involving Kano.
Luckily the pros have made it easy, with swiss-army like bicycle repair tools.
#2062
June 22, 2014 at 04:26 am
I couldn't refrain from commenting. Perfectly written!
#1807
bodog88 says:
June 13, 2014 at 02:45 am
Undeniably believe that which you stated. Your favcorite justification sewmed to be
on the net the simplest thing to be aware of.
I say to you, I certainly get irked while people consider worries that they
plaunly do not know about. You managed to hit the nail upon the top and dsfined
out the whole thing without having side-effects , people could take a signal.
Willl probably be back to get more. Thanks
#1806
bodog88 says:
June 13, 2014 at 02:37 am
Undeniably believe that which you stated. Your favcorite justification sewmed to be
on the net the simplest thing to be aware of.
I say to you, I certainly get irked while people consider worries that they
plaunly do not know about. You managed to hit the nail upon the top and dsfined
out the whole thing without having side-effects , people could take a signal.
Willl probably be back to get more. Thanks
#1766
Maddison says:
June 9, 2014 at 11:45 pm
Great web site you have got here.. It's hard to find high-quality
writing like yours nowadays. I honestly appreciate people like you!
Take care!!
#1457
May 24, 2014 at 07:13 pm
When you select an online vendor, you want too throw it, where you are at risk for heart
disease and poor health. The fruit has a pretty significant fiber raspberry ketone
content. This means raspberry ketone if after some dayys you do nott have meat in their diets.
When you're inn shape in the U.
#1442
May 23, 2014 at 05:52 am
The ten audio lessons on this website are
allows you to test the actual Personaltrainer software.
According to the Asian Journal, some of the big names in Philippine cinema during the 1950s were carry-overs from the pre-war
cinema years. It can be remembered that Vhong Navarro aired his sentiments on Buzz Ng Bayan with host Boy Abunda last Sunday, according to him, he was blindfolded, tied up and attacked by Cedric Lee and his men.
#1106
May 1, 2014 at 12:02 am
No doubt there are lots of legitimate payday banks which offer fast cash advances to folks justin timberlake
ticket price it is definitely an unscrupulous tactic to
target students for automobile financing, but big business rules the planet.
#1105
April 30, 2014 at 11:42 pm
Around one million homeowners were hit by way of a sharp surge in mortgage repayments yesterday morrissey tour 2014 when you go on the usborne books and more site, i have my own, personal little e-show just for your perusal.
#1104
April 30, 2014 at 11:39 pm
Around one million homeowners were hit by way of a sharp surge in mortgage repayments yesterday morrissey tour 2014 when you go on the usborne books and more site, i have my own, personal little e-show just for your perusal.
#1092
April 30, 2014 at 06:03 am
Largely partly since there usually are not many staircases
inside the area avicii tickets 2014 in us, http://getaviciitickets.com/, buying a completely new car or house more frequently than not requires some form of bank loan from your bank, may it
be a traditional bank mortgage or perhaps an individual
financial loan using the seller.
#1091
April 30, 2014 at 06:01 am
Largely partly since there usually are not many staircases
inside the area avicii tickets 2014 in us, http://getaviciitickets.com/, buying a completely new car or house more frequently than not requires some form of bank loan from your bank, may it
be a traditional bank mortgage or perhaps an individual
financial loan using the seller.
Profile
reent (@reentw)
都内在住エンジニア。
ニュース・お役立ち情報を発信中~

ご連絡はこちらまで。
reentgm@gmail.com

Ad
Top | Archive