我正在尝试创建一个Instagram媒体下载程序,在这里我使用了下面的代码,但是它给了我这一错误。有人能告诉我我哪里错了吗?从6-7小时开始我就被困在这里面了。
错误上说-
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 18index.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>哪里出问题了?第13行的错误是什么?
编辑1:
将代码更新为第13行中的以下代码:
$arr = $json['graphql']['shortcode_media']['display_resources'];还收到另一个警告-
Warning: count(): Parameter must be an array or an object that implements Countable in C:\xampp\htdocs\proj-2\index.php on line 16发布于 2022-11-08 10:08:23
可能存在json_decode()错误,因为参数不是有效的JSON字符串。您可以使用json_last_error()检查解码成功与否。
我希望这个代码片段会有所帮助:
$json = json_decode($json);
if (JSON_ERROR_NONE !== json_last_error()) {
throw new Exception('json_decode failed: ' . json_last_error_msg());
}https://stackoverflow.com/questions/74356819
复制相似问题