PHP - 使用简单的php从instagram页面获取帖子的内容

时间:2017-07-27 14:58:22

标签: php html curl instagram

有没有办法使用简单the contents of an post

获取 Instagram页面 php

我做了一些搜索,我找到了这个脚本

 $url = 'https://www.instagram.com/pagename/';
 $str = file_get_contents($url);
 $count = 0;
 if(preg_match('#followed_by": {"count": (.*?)}#', $str, $match)) {
    $count = $match[1]; // get the count from Regex pattern
  } 
echo $count;

但是只有一个方法才能获得追随者的数量 使用相同的概念来获取 Instagram帖子的内容

2 个答案:

答案 0 :(得分:0)

这是一个有效的代码(今天)。但正如@Andy所说,它不可靠而且它很脏:)

<?php

$source = file_get_contents("https://www.instagram.com/p/POST_ID/");

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

if (!isset($matches[1]))
    return false;

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

print_r($r);

// Example to get the likes count
// $r->entry_data->PostPage[0]->graphql->shortcode_media->edge_media_preview_like->count

答案 1 :(得分:0)

我接受了@Andy的建议,因为它不可靠而且很脏。

所以这就是我发现的html

  

Instagram更改了他们的页面标记,您的应用程序将会中断。

是这个

    $username = 'username';
    $instaResult= 
    file_get_contents('https://www.instagram.com/'.$username.'/media/');
   //decode json string into array

  $data = json_decode($instaResult);

foreach ($data as $posts) {
   foreach($posts as $post){
     $postit = (array) json_decode(json_encode($post), True);
     /* get post text and image */
              echo '<p>' .$postit["caption"]["text"].'</p>';
              echo '<img src="'.$postit["images"]["standard_resolution"]["url"].'" />';
              echo "</br>-----------</br>";
     }
   }