获取 Instagram post 内容 php

Get Instagram post contents php

我想开发一个 PHP 项目来获取 Instagram post 的内容(通过 post URL)。我的意思是,例如,爬虫获取此 Url 并返回 :

#TBT Used to love carrying them both at once #twinmom #mycoconuts 
#mylifeinmyhands #toobignow #istilltrysometimes #LOVE #forevermybabies

我用了file_get_contents之后,返回的response是空的。我不知道如何解决这个问题。你能帮帮我吗?

我不想使用 Instagram API!

你不应该使用它,因为它会在 Instagram 更改其标记后立即中断,但你可以:

<?php

$instagram_url = "https://www.instagram.com/p/BX6IowXFbq9";

$source = file_get_contents($instagram_url);

preg_match('/<script type="text\/javascript">window\._sharedData =([^;]+);<\/script>/', $source, $matches);

$data = json_decode($matches[1]);

echo $data->entry_data->PostPage[0]->graphql->shortcode_media->edge_media_to_caption->edges[0]->node->text;

这有点晚了,但我能够获取 Instagram 时间线、特定媒体详细信息。

例如:

  • 获取 post
  • 的用户列表

如果 Instagram 用户页面 url 是 https://www.instagram.com/marvel 那么要获取此页面数据,您可以使用下面的 URL

https://www.instagram.com/marvel?__a=1
  • 获取特定媒体post详细信息

如果 Instagram post 详细页面 url 是 https://www.instagram.com/p/BfeD8RxHwnC/?taken-by=marvel 那么要获取此页面数据,您可以使用下面的 URL

https://www.instagram.com/p/BfeD8RxHwnC/?taken-by=marvel&__a=1