PHP获取Instagram照片点赞用户列表失败,页面空白求解决
问题:PHP获取Instagram照片点赞用户列表页面空白
我希望通过PHP获取Instagram照片的点赞用户列表,但页面显示空白。以下是我的代码:
function Connect($url){ $curl=curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl,CURLOPT_RETURNTRANSFER,true); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); $data=curl_exec($curl); echo curl_error($curl); return str_replace(array("\n","\t","\r"),null,$data); } $bag=Connect("https://www.instagram.com/p/BfnPaG-HYrq/liked_by/"); preg_match_all('#<a class="_2g7d5 notranslate _o5iw8 " title="(.*?)" href="(.*?)">(.*?)</a>#', $bag, $list); $like=$list[1]; for ($i=0; $i < count($like); $i++) { echo "<ul>"; echo "<li>"; echo $like[i]; echo "</li>"; echo "</ul>"; }
请问该如何解决?谢谢。
解决方案
咱们先拆解下导致页面空白的核心问题,再给出可行的修正方案:
1. 先排查基础错误
首先,你的代码里有个明显的语法问题:循环中的echo $like[i];应该是echo $like[$i];——少了美元符号,这会直接触发PHP报错,如果你的服务器没开启错误显示,页面就会直接空白。
先在代码开头加上错误提示,方便后续排查:
error_reporting(E_ALL); ini_set('display_errors', 1);
2. 核心问题:Instagram页面结构已完全变化
你用的正则表达式是针对旧版Instagram的静态HTML结构,但现在Instagram是通过JavaScript动态渲染内容,你直接请求/liked_by/页面拿到的静态HTML里,根本没有你要的<a>标签结构,preg_match_all自然抓不到任何数据,$list[1]是空数组,循环也就不会输出内容。
3. 修正后的实现思路
现在想要获取点赞列表,需要模拟浏览器请求Instagram的GraphQL接口,直接拿JSON格式的返回数据,步骤如下:
示例代码
error_reporting(E_ALL); ini_set('display_errors', 1); function Connect($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 用标准浏览器UA,避免被识别为爬虫 curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36'); // 带上登录后的Cookie(可以手动登录Instagram后,从浏览器复制Cookie内容保存到文件) curl_setopt($curl, CURLOPT_COOKIE, 'your_instagram_cookie_here'); $data = curl_exec($curl); if(curl_errno($curl)) { echo 'Curl请求错误: ' . curl_error($curl); exit; } curl_close($curl); return $data; } // 步骤1:从帖子页面提取必要的共享数据 $postPage = Connect("https://www.instagram.com/p/BfnPaG-HYrq/"); preg_match('/window._sharedData = (.*?);<\/script>/', $postPage, $matches); if(empty($matches[1])) { echo '无法获取帖子基础数据'; exit; } $sharedData = json_decode($matches[1], true); $shortcode = $sharedData['entry_data']['PostPage'][0]['graphql']['shortcode_media']['shortcode']; // 步骤2:请求点赞列表的GraphQL接口(注意:query_hash可能会随Instagram更新失效,需要自己抓最新的) $queryHash = 'd5d763b1e2acf209d62d22d184488e57'; $apiUrl = "https://www.instagram.com/graphql/query/?query_hash={$queryHash}&variables={\"shortcode\":\"{$shortcode}\",\"first\":50}"; $likeResponse = Connect($apiUrl); $likeData = json_decode($likeResponse, true); // 步骤3:输出点赞用户列表 if(isset($likeData['data']['shortcode_media']['edge_liked_by']['edges'])) { echo '<ul>'; foreach($likeData['data']['shortcode_media']['edge_liked_by']['edges'] as $item) { $username = $item['node']['username']; echo "<li>{$username}</li>"; } echo '</ul>'; } else { echo '无法获取点赞列表:可能query_hash已失效,或请求被Instagram限制'; }
4. 重要注意事项
- query_hash会失效:Instagram的GraphQL接口参数经常更新,你需要用浏览器开发者工具(Network面板)抓最新的点赞列表请求,替换代码里的
query_hash。 - 防限制措施:频繁请求会被Instagram封禁IP,建议加上请求间隔,并且使用登录后的Cookie(模拟真实用户行为)。
- 官方API优先:如果是长期项目,最好申请Instagram官方开发者账号,使用正式API,稳定性更高。
内容的提问来源于stack exchange,提问作者ucnumara
相关产品推荐
相关产品推荐

