0

I'm trying to create an Instagram media downloader where I'm using the following code but it's giving me this error. Can someone tell me where am I wrong? I'm stuck in this since last 6-7 hrs.

The error says -

Notice: Trying to get property 'graphql' of non-object in C:\xampp\htdocs\proj-2\index.php on line 13

Notice: Trying to get property 'shortcode_media' of non-object in C:\xampp\htdocs\proj-2\index.php on line 13

Notice: Trying to get property 'display_resources' of non-object in C:\xampp\htdocs\proj-2\index.php on line 13

Warning: count(): Parameter must be an array or an object that implements Countable in C:\xampp\htdocs\proj-2\index.php on line 18

index.php

<?php
$html = "";

//Getting Json File From URL?__a=1
if(isset($_GET['url'])) {
    $json = file_get_contents($_GET['url']."?__a=1");
    //Getting the file content
    $json = json_decode($json);
    //Converting the JSON into Php object

    $arr = $json->graphql->shortcode_media->display_resources;


    for($i=0;$i<count($arr);$i++ ) {
        $html .= '<img src="'.$arr[$i]->src.'" > <br><br> <a href="'.$arr[$i]->src.'" download >Download</a><hr>';
    }
}

?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Instagram Photo Downloader</title>
</head>
<body>
    <h1>Instagram Downloader</h1>
    <form action="" method="get">
    <input type="text" name="url" id="">
    <button type="submit">DOWNLOAD</button>
    </form>

    <div class="image">
    <?php echo $html ; //Showing all Stored Images ?>
    </div>
</body>
</html>

Where is thing going wrong? What's the error in that Line 13?


Edit 1:

Updated my code to following in Line 13:

$arr = $json['graphql']['shortcode_media']['display_resources'];

Still getting another warning -

Warning: count(): Parameter must be an array or an object that implements Countable in C:\xampp\htdocs\proj-2\index.php on line 16
  • `json_decode` might be returning false, which means it cannot decode the value. Check what the value of `$json` is. – Nigel Ren Nov 08 '22 at 07:21
  • Nothing got printed when tried to print it. [Check my code here](https://imgur.com/w1jgOzv) – Nitin Kumar Nov 08 '22 at 07:48
  • Nothing gets printed when trying to print it. `echo "

    " . $json . "

    ";`.
    – Nitin Kumar Nov 08 '22 at 07:49
  • Updated my code & now it's giving another error - `Warning: count(): Parameter must be an array or an object that implements Countable in C:\xampp\htdocs\proj-2\index.php on line 16`. Checkout my updated code [here](https://imgur.com/l4wJSgi). – Nitin Kumar Nov 08 '22 at 08:14

1 Answers1

0

Possibly there is a json_decode() error because the parameter is not a valid JSON string. You can check the decoding success with the json_last_error().

I hope this code snippet will help:

$json = json_decode($json);
if (JSON_ERROR_NONE !== json_last_error()) {
    throw new Exception('json_decode failed: ' . json_last_error_msg());
}
hNczy
  • 305
  • 1
  • 8
  • Getting following error - `Fatal error: Uncaught Exception: json_decode failed: Syntax error in C:\xampp\htdocs\proj-2\index.php:12 Stack trace: #0 {main} thrown in C:\xampp\htdocs\proj-2\index.php on line 12` – Nitin Kumar Nov 08 '22 at 11:27
  • So it is sure there is some error in the input string. You can check the JSON string after the `file_get_contents` and before the `json_decode`. For example you can use `var_dump($json);` – hNczy Nov 08 '22 at 12:17
  • However, I read about the URL in your code with the `?__a=1` ending. It was deprecated in 2018 so I think today it isn't working anymore. You need a more complicated method to use. Please see https://developers.facebook.com/docs/instagram-api/getting-started. – hNczy Nov 08 '22 at 12:29