使用 php 从 instagram __a=1 阅读 JSON

Read JSON with php from instagram __a=1

原创

首先要强调的是,这是我在PHP中的第一个脚本,所以很多地方都可以改进,但现在我只需要它能工作! 我在 php 中创建了这个脚本,以从位于 https://www.instagram.com/{{username}}/?[=33 的 public instagram json 文件中获取 public 个人资料信息=]=1 在本地尝试,一切正常,但将其托管在网站 file_get_contents($ url) 上不起作用(第 29 行),我尝试使用 CURL 读取文件,但它不起作用无论如何都不起作用,它没有正确读取 json 文件,试图对他读取的内容进行回显,instagram 徽标出现在站点屏幕上。 我该如何解决?

更新

我刚刚注意到,如果我尝试对任何配置文件 www.instagram.com/USERNAME 的 link 创建 file_get_contents (),它会给我完全相同的结果,可能是试图阅读 www.instagram.com/USERNAME/?__a= 1 instagram 通知并将我重定向到个人资料页面?

我已经尝试 htmlentities() 我通过 file_get_contents 收到的数据 ... tatan ..实际上脚本读取了一个奇怪的 html 页面,但未找到在我给的地址!

<?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;
}




?>

我相信这是 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_VERIFYHOSTCURLOPT_SSL_VERIFYHOST.