Php Instagram故事刮刀
我制作了一个Instagram的小故事刮板,但我不能让它工作…我可能太累了。 我什么都试过了,但自己解决不了这个问题 每次我用Wamp运行它,都会得到相同的错误 这是我的index.php文件Php Instagram故事刮刀,php,instagram,scrape,Php,Instagram,Scrape,我制作了一个Instagram的小故事刮板,但我不能让它工作…我可能太累了。 我什么都试过了,但自己解决不了这个问题 每次我用Wamp运行它,都会得到相同的错误 这是我的index.php文件 <?php require_once('instagramStory.php'); $story = new instagram_story(); echo $story->getStory("garyvee"); ?> 这是instagramStory.php <?php
<?php
require_once('instagramStory.php');
$story = new instagram_story();
echo $story->getStory("garyvee");
?>
这是instagramStory.php
<?php
class instagram_story{
protected function file_get_contents_curl($url){
$cookies = dirname(__FILE__)."/cookie.txt" ;
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($curl, CURLOPT_COOKIEFILE, $cookies);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, true);
$answer = curl_exec($curl);
curl_close($curl);
return $answer;
}
public function getStory($username){
$url = file_get_contents("https://www.instagram.com/$username/");
$json = '/sharedData\s=\s(.*[^\"]);<.script>/ixU';
preg_match_all($json, $url, $jsondata, PREG_SET_ORDER, 0);
$array = json_decode($jsondata[0][1], true);
$user_id = $array['entry_data']['ProfilePage']['0']['graphql']['user']['id'];
$stories = $this->file_get_contents_curl("https://www.instagram.com/graphql/query/?query_hash=de8017ee0a7c9c45ec4260733d81ea31&variables=%7B%22reel_ids%22%3A%5B%22$user_id%22%5D%2C%22tag_names%22%3A%5B%5D%2C%22location_ids%22%3A%5B%5D%2C%22highlight_reel_ids%22%3A%5B%5D%2C%22precomposed_overlay%22%3Afalse%2C%22show_story_viewer_list%22%3Atrue%2C%22story_viewer_fetch_count%22%3A50%2C%22story_viewer_cursor%22%3A%22%22%7D");
$data = json_decode($stories, true);
$stories = $data['data']['reels_media']['0']['items'];
$_story = [];
foreach ($stories as $story) {
$vid = 'video_resources';
if (!array_key_exists($vid, $story)) {
$_story [] = $story['display_url'];
} else {
$_story [] = $story['video_resources'][0]['src'];
}
}
foreach ($_story as $story) {
$check = '/mp4/m';
preg_match_all($check, $story, $matches, PREG_SET_ORDER, 0);
if (empty($matches)) {
echo "<a href=$story&dl=1><img src=$story></a>";
} else {
echo '<video width="320" height="240" controls>';
echo '<source src="' . $story . '" type="video/mp4">';
echo '</video>';
echo "<a href=$story&dl=1>Download</a>";
}
}
}
}
?>
您之所以会出现此错误,是因为您试图在非数组的对象上循环 在第22行,你要做的是:
$stories=$data['data']['reels_media']['0']['items']代码>
这显然是错误的。尝试var\u dump($data)
并查看您从中获得的输出。如果您仍在努力发布var_转储的结果。我正在与您检查相同的问题。显然,这是因为命令“foreach”正在访问一个空数组
我深入检查了数据,发现$stories
是空的,因为它无法从instagram网站接收当前数据
当我尝试访问由程序在我自己的浏览器上创建的URL(登录ins帐户)时,它会工作
所以问题是因为代码$cookies=dirname(_文件)“/cookie.txt”代码>找不到当前cookie。您应该在登录instagram网站后复制cookie,并将cookie.txt
文件与.php文件放在同一文件夹中。print\r($data);死亡代码>就在您的json\u编码之后
。你得到了什么?它显然不喜欢$stories
<代码>变量转储($stories)
在第24行之前查看它包含的内容。我仍在挣扎