Skip to main content

Instagram story scraper didn't work with curl or file_get_contents method in php

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>

HTML OUTPUT: HERE

POST HERE

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

Popular posts from this blog

Where and how is this Laravel kernel constructor called? [closed]

Where and how is this Laravel kernel constructor called? public fucntion __construct(Application $app, $Router $roouter) { } I have read the documentation and some online tutorial but I can find any clear explanation. I am learning Laravel and I am wondering where does this kernel constructor receives its arguments from. "POSTMOTERM" CLARIFICATION: Here is more clarity.I have checked the boostrap/app.php and it is only used for boostrapping the interfaces into the container class. What is not clear to me is where and how the Kernel class is instatiated and the arguments passed to the object calling the constructor.Something similar to; obj = new kernel(arg1,arg2) or, is the framework using some magic functions somewhere? Special gratitude to those who burn their eyeballs and brain cells on this trivia before it goes into a full blown menopause alias "MARKED AS DUPLICATE". To some of the itchy-finger keyboard warriors, a.k.a The mods,because I believe in th...

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console...

Confusion between commands.Bot and discord.Client | Which one should I use?

Whenever you look at YouTube tutorials or code from this website there is a real variation. Some developers use client = discord.Client(intents=intents) while the others use bot = commands.Bot(command_prefix="something", intents=intents) . Now I know slightly about the difference but I get errors from different places from my code when I use either of them and its confusing. Especially since there has a few changes over the years in discord.py it is hard to find the real difference. I tried sticking to discord.Client then I found that there are more features in commands.Bot . Then I found errors when using commands.Bot . An example of this is: When I try to use commands.Bot client = commands.Bot(command_prefix=">",intents=intents) async def load(): for filename in os.listdir("./Cogs"): if filename.endswith(".py"): client.load_extension(f"Cogs.{filename[:-3]}") The above doesnt giveany response from my Cogs ...