I'm trying to fetch the story json data of instagram public account using curl or file_get_contents methods but unfortunately got no luck so far. As I figure, I cannot fetch the json data from the instagram url. Somehow instagram doesn't allow me to get the story data.
FOR TEST
User ID: 22009575863
We can access the test url directly: https://www.instagram.com/graphql/query/?query_id=17873473675158481&variables=%7B%22precomposed_overlay%22%3Afalse%2C%22reel_ids%22%3A22009575863%7D
But cannot get the file contents(json) using curl method and this is my problem. Somehow getContents($url, $cookies = true) not return any story data but accessing the url directly return successful.
HTML
<div class="form-group">
<label><?php language('_PLACEHOLDER2'); ?></label>
<input type="url" id="input" name="input" class="form-control" placeholder="<?php language('_PLACEHOLDER2'); ?>">
<input type="hidden" name="action" id="action" value="story">
<input type="hidden" name="json" id="json" value="">
<input type="hidden" name="token" id="token" value="<?php echo $_SESSION['token']; ?>">
<button id="btnDownloadStory" class="btn btn-default btn-block mt-3">
<i class="fas fa-download"></i> <?php language('_DOWNLOAD'); ?>
</button>
</div>
PHP USING SWITCH CASE
switch ($_POST['action']) {
case 'story':
if (isset($json['graphql']['user']['username']) != '') {
$data['user'] = $instagram->getProfileFromData($json['graphql']['user']);
} else {
$data['user'] = $instagram->getProfile($username, true);
}
$data['medias'] = $instagram->getStories($data['user']['id']);
break;
default:
returnError('Invalid action.');
break;
}
/**
* @param int $userId
* @return array
*/
public function getStories($userId)
{
$baseUrl = 'https://www.instagram.com/graphql/query/?query_id=17873473675158481&variables=';
$variables = '{"precomposed_overlay":false,"reel_ids":["' . $userId . '"]}';
$stories = $this->getContents($baseUrl . urlencode($variables), true);
$data = json_decode($stories, true);
if (isset($data['data']['reels_media'][0]['items']) == '') {
return array();
}
$stories = $data['data']['reels_media'][0]['items'];
$medias = array();
foreach ($stories as $story) {
switch ($story['__typename']) {
case 'GraphStoryImage':
$i = count($story['display_resources']) - 1;
array_push($medias, array(
'type' => 'image',
'fileType' => 'jpg',
'url' => $story['display_resources'][$i]['src'],
'downloadUrl' => $story['display_resources'][$i]['src'] . '&dl=1'
));
break;
case 'GraphStoryVideo':
$i = count($story['video_resources']) - 1;
array_push($medias, array(
'type' => 'video',
'fileType' => 'mp4',
'preview' => $story['display_url'],
'url' => $story['video_resources'][$i]['src'],
'downloadUrl' => $story['video_resources'][$i]['src'] . '&dl=1'
));
break;
}
}
return $medias;
}
Instragram.php
$cookieFile = __DIR__ . '/../storage/cookie.txt';
$userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36';
/**
* @param string $url
* @param bool $cookies
* @return string
*/
public function getContents($url, $cookies = true)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Cookie ' . file_get_contents($this->cookieFile)
));
$data = curl_exec($ch);
curl_close($ch);
return $data; //return empty here
}
source https://stackoverflow.com/questions/68145348/instagram-story-scraper-didnt-work-with-curl-or-file-get-contents-method-in-php


Comments
Post a Comment