【发布时间】:2020-08-18 00:45:35
【问题描述】:
原创
我想首先强调的是,这是我的第一个 PHP 脚本,有很多地方可以改进,但现在我只需要它工作! 我在 php 中创建了这个脚本,以从位于 https://www.instagram.com/{{username}}/?__a=1 的公共 instagram json 文件中获取公共个人资料信息 在本地尝试,一切正常,但将其托管在网站上 file_get_contents($ url) 不起作用(第 29 行),我尝试使用 CURL 读取文件,但它无论如何都不起作用,它没有正确读取 json 文件,尝试回显他读取的内容,instagram 徽标出现在站点屏幕上。 我该如何解决?
更新
我刚刚注意到,如果我尝试对任何配置文件 www.instagram.com/USERNAME 的链接进行 file_get_contents(),它会给我完全相同的结果,可能是试图阅读 www.instagram.com/ USERNAME/?__a= 1 instagram 通知并将我重定向到个人资料页面?
我已经在通过 file_get_contents ... tatan 收到的数据上尝试了 htmlentities()
<?php
$commentiPost;
$likePost;
$postData;
$image;
$urlprofilo;
$followers;
$username;
$follow;
$like;
$commenti;
function getMediaByUsername($count) {
global $image;
global $commentiPost;
global $likePost;
global $urlprofilo;
global $followers;
global $username;
global $follow;
global $postData;
global $like;
global $commenti;
$uname = htmlspecialchars($_GET["name"]);
$username = strtolower(str_replace(' ','_',$uname));
$url = "https://www.instagram.com/".$username."/?__a=1";
$userinfo = file_get_contents($url);
$userdata = json_decode($userinfo,true);
$user = $userdata['graphql']['user'];
$iteration_url = $url;
if(!empty($user)){
$followers = $user['edge_followed_by']['count'];
$follow = $user['edge_follow']['count'];
$fullname = $user['full_name'];
$username = $user['username'];
$profilepic = $user['profile_pic_url'];
$profilepic = (explode("/",$profilepic));
$urlprofilo = "https://scontent-frt3-1.cdninstagram.com/v/t51.2885-19/s150x150/$profilepic[6]";
$limit = $count;
$tryNext = true;
$found = 0;
while ($tryNext) {
$tryNext = false;
$remote = file_get_contents( $iteration_url );
$response = $remote;
if ($response === false) {
return false;
}
$data = json_decode($response, true);
if ( $data === null) {
return false;
}
$media = $data['graphql']['user']['edge_owner_to_timeline_media'];
foreach ( $media['edges'] as $index => $node ) {
if ( $found + $index < $limit ) {
if (isset($node['node']['is_video']) && $node['node']['is_video'] == true) {
$type = 'video';
} else {
$type = 'image';
}
$like = $like + $node['node']['edge_liked_by']['count'];
$commenti = $commenti + $node['node']['edge_media_to_comment']['count'];
$image[] = array( "<a href=".$node['node']['display_url'].">
<img src=".$node['node']['display_url']." alt="." />
<h3>Like: </strong>".$node['node']['edge_liked_by']['count']."</strong> Commenti: <strong>".$node['node']['edge_media_to_comment']['count']."</strong></h3>
</a>");
$postData[] = array(" '".gmdate("d-m-Y",$node['node']['taken_at_timestamp'])."',");
$likePost[] = array(" ".$node['node']['edge_liked_by']['count'].",");
$commentiPost[] = array(" ".$node['node']['edge_media_to_comment']['count'].",");
}
}
$found += count($media['edges']);
if ( $media['page_info']['has_next_page'] && $found < $limit ) {
$iteration_url = $url . '&max_id=' . $media['page_info']['end_cursor'];
$tryNext = true;
}
}
} else{
}
}
getMediaByUsername( 12);
if(isset($image))
{
$postTot = count($image);
}
else {
$postTot = 0;
}
if($postTot > 0 and $followers > 0){
$ER = round(((($like + $commenti)/$postTot)/$followers)*100, 1);
}
else {
$ER = 0;
}
?>
【问题讨论】:
-
你说读取不正确。您是否从 url 获得任何响应/数据?你得到什么回应?
-
那个 CURL 请求在哪里? :)
-
@bestprogrammerintheworld 我知道查看是否得到任何响应的唯一方法是回显 $userinfo 变量(第 29 行),这样做会在页面中心显示 instagram 图标,并且网页标题更改为“instagram login”
-
@VirCom 我试过 `function url_get_contents ( $url ) { if ( ! function_exists( 'curl_init' ) ) { die( 'The cURL library is not installed.' ); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true ); $output = curl_exec($ch); curl_close($ch);返回$输出; } ` 而不是 file_get_contents($url)