Search code examples
phpjsoninstagram

Read JSON with php from instagram __a=1


original

I want to emphasize first that it is my first script in PHP, so many things can be improved, but for now I just need it to work! I created this script in php to get public profile information from the public instagram json file located at https://www.instagram.com/{{username}}/?__a=1 trying it locally, everything works correctly, but hosting it on a website file_get_contents($ url) doesn't work (line 29) , I tried to use CURL to read the file, but it doesn't work anyway, it doesn't read the json file correctly, trying to do an echo of what he reads the instagram logo appears on the site screen. how can I solve it?

update

I just noticed that if I try to make file_get_contents () of a link of any profile www.instagram.com/USERNAME, it gives me the exact same result, it may be that trying to read www.instagram.com/USERNAME/?__a= 1 instagram notice and redirect me to the profile page?

I've tried htmlentities() on the data I receive through file_get_contents ... tatan .. actually the script reads a strange html page that is NOT found at the address I gave it!

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




?>

Solution

  • I belive that is SSL certificate problem. When you modify your function to:

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

    Probably you will see as a result: Curl error: SSL certificate problem: unable to get local issuer certificate. Add that certificate to your system or uncomment lines with options: CURLOPT_SSL_VERIFYHOSTand CURLOPT_SSL_VERIFYHOST.