【问题标题】:Read JSON with php from instagram __a=1从 instagram __a=1 用 php 读取 JSON
【发布时间】: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)

标签: php json instagram


【解决方案1】:

我相信这是 SSL 证书问题。当您将函数修改为:

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 );
    // curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false);
    // curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
    $output = curl_exec( $ch );

    if(curl_errno( $ch )) {
        die ('Curl error: ' . curl_error($ch));
    }

    curl_close( $ch );
    return $output;
}

您可能会看到这样的结果:Curl error: SSL certificate problem: unable to get local issuer certificate。 将该证书添加到您的系统或使用以下选项取消注释行:CURLOPT_SSL_VERIFYHOST和CURLOPT_SSL_VERIFYHOST。

【讨论】:

  • 嗯...我将该代码粘贴到在线编辑器并在线共享:paiza.io/projects/e/EhkE3Jmb5QCUNpLkisJa8Q?theme=twilight 并且可以正常工作并返回预期的 JSON 数据...
  • 这怎么可能??我创建了一个文件,在其中复制了您发送的代码!还有一个我在其中创建了 url file_get_contents (),这些是带有输出的链接 gorinsights.com/file_get_contents.php gorinsights.com/cURL.php 和 cURL 我没有得到任何输出提示,这怎么可能?
  • 我认为是 cURL 扩展问题。您在 php 错误日志文件或系统日志文件中看到任何信息吗?基于 cURL 的函数返回任何值或停止在其中一行中执行脚本并死掉?
猜你喜欢
  • 2018-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-11
  • 1970-01-01
  • 2018-08-22
  • 1970-01-01
相关资源
最近更新 更多