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

PHP - Get the contents of an post from instagram page using simple php

有没有办法使用简单的 php

获取 instagram 页面 the contents of an post

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

 $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 post 的内容?

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

<?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

我采纳了@Andy 的建议,因为他的建议不可靠而且很脏

这就是我在 html

中找到的内容

Instagram change their page markup, your application will break.

这是

    $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>";
     }
   }