首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取错误:在PHP脚本中尝试获取非对象的属性“graphql”

获取错误:在PHP脚本中尝试获取非对象的属性“graphql”
EN

Stack Overflow用户
提问于 2022-11-08 07:05:45
回答 1查看 35关注 0票数 0

我正在尝试创建一个Instagram媒体下载程序,在这里我使用了下面的代码,但是它给了我这一错误。有人能告诉我我哪里错了吗?从6-7小时开始我就被困在这里面了。

错误上说-

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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>

哪里出问题了?第13行的错误是什么?

编辑1:

将代码更新为第13行中的以下代码:

代码语言:javascript
复制
$arr = $json['graphql']['shortcode_media']['display_resources'];

还收到另一个警告-

代码语言:javascript
复制
Warning: count(): Parameter must be an array or an object that implements Countable in C:\xampp\htdocs\proj-2\index.php on line 16
EN

回答 1

Stack Overflow用户

发布于 2022-11-08 10:08:23

可能存在json_decode()错误,因为参数不是有效的JSON字符串。您可以使用json_last_error()检查解码成功与否。

我希望这个代码片段会有所帮助:

代码语言:javascript
复制
$json = json_decode($json);
if (JSON_ERROR_NONE !== json_last_error()) {
    throw new Exception('json_decode failed: ' . json_last_error_msg());
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74356819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档