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

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...

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype

The number of nodes and edges I calculated manually and by a method provided by networkx library dont match

I am trying to analyse a graph using networkx library in Python. I have implemented a function that returns the nodes and edges of that graph. After that I counted the number of nodes and edges by implementing the len() function for nodes and edges, which gave me 17 for both nodes and edges. However, when I used the method graph.number_of_nodes() of networkx library it gave me 53. My code: import pandas as pd import networkx as nx import matplotlib.pyplot as plt header_list = ["a","b","w"] E =pd.read_csv("aves-barn-swallow-contact-network.edges",sep=" ",header= None,names=header_list) # print(E.head()) # prints first few rows of the csv file G = nx.from_pandas_edgelist(E,"a","b",["w"]) nx.draw(G) plt.show() class GraphAnalyse: #finding nodes and edges def nodeEdge(self,Graph): nodes = Graph.nodes() edges = Graph.edges() ...