【问题标题】:PHP - Get the contents of an post from instagram page using simple phpPHP - 使用简单的 php 从 instagram 页面获取帖子的内容
【发布时间】:2017-07-27 14:58:22
【问题描述】:

有没有办法使用简单的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 帖子的内容

【问题讨论】:

  • 不清楚你在问什么。 “发布数据”是指 HTTP POST 数据,还是 Instagram 帖子的内容?无论如何,这似乎是一种不好的做事方式。有一些库和 API 用于通过网络服务与 Instagram 进行交互 - 谷歌它们。
  • 我现在有 API,但我现在想知道是否可以像上面的例子那样做
  • 您在上面的示例中所做的只是读取 URL 的整个 HTML 源代码,然后搜索一些标记以尝试提取一些内容。所以答案是,是的,如果你能弄清楚如何提取你想要的特定内容。但这是一种糟糕且不可靠的做法——例如,如果 Instagram 更改他们的页面标记,您的应用程序就会崩溃。使用适当的库并正确地做事,例如github.com/cosenary/Instagram-PHP-API
  • @Andy 谢谢,我不能说更多了

标签: php html curl instagram


【解决方案1】:

这是一个有效的代码(今天)。但正如@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

【讨论】:

    【解决方案2】:

    我接受了@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>";
         }
       }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-23
      • 1970-01-01
      • 2016-05-23
      相关资源
      最近更新 更多