How To Scrape Instagram Profile Using PHP

Instagram scrapping is something which is not easy. They block the request , limit the request with rate limiting and prevent to scape instagram users profile. In this post , we will learn how to scrape instagram using php

Scraping Instagram With PHP

Why do we need to scarpe instagram ? The answer is to get details of profile. The details may be profile picture , total post count , total video / reels posted , total followers count , total following , profile is private or public , profile is verified or not etc. For creating a instagram scraper , the requirements are as follows
  • Localhost Server
  • XAMPP / WAMPP
  • Text Editor , Prefer VSCode
  • JSON Knowledge
  • JSON Beautifier ( Link )

PHP Scrape Instagram Profile Detail

Create a new file , lets say index.php in which the codes will be written. Declare a variable $user in which the username will be there of whom we want to extract information. After that , api is most important. The api for extracting instagram user information is https://i.instagram.com/api/v1/users/web_profile_info/?username=

Also , we need valid header value to extract information from instagram. I am providing three headers which are working
$header = "Mozilla/5.0 (iPhone; CPU iPhone OS 12_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 Instagram 105.0.0.11.118 (iPhone11,8; iOS 12_3_1; en_US; en-US; scale=2.00; 828x1792; 165586599)";
//OR
$header="Mozilla/5.0 (Linux; Android 9; GM1903 Build/PKQ1.190110.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/75.0.3770.143 Mobile Safari/537.36 Instagram 103.1.0.15.119 Android (28/9; 420dpi; 1080x2260; OnePlus; GM1903; OnePlus7; qcom; sv_SE; 164094539)";
//OR
$header="User-Agent':'Instagram 76.0.0.15.395 Android (24/7.0; 640dpi; 1440x2560; samsung; SM-G930F; herolte; samsungexynos8890; en_US; 138226743)";
By using any one of the header , we can scrape instagram

PHP Code To Extract Instagram User Detail

Below is the complete php code to scrape instagram user detail.


<?php
$user="thebestnd";
$api="https://i.instagram.com/api/v1/users/web_profile_info/?username=";
$header="User-Agent':'Instagram 76.0.0.15.395 Android (24/7.0; 640dpi; 1440x2560; samsung; SM-G930F; herolte; samsungexynos8890; en_US; 138226743)";
$ch = curl_init($api.$user);
curl_setopt($ch, CURLOPT_USERAGENT, $header);
curl_setopt($ch, CURLOPT_REFERER, 'https://www.instagram.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("x-ig-app-id: 567067343352427"));
$result = curl_exec($ch);
curl_close($ch);
$result=json_decode($result);
$status=$result->status;

if ($status=="ok")
{
    $username=$result->data->user->username;
    $fullname=$result->data->user->full_name;
    $isprivate=$result->data->user->is_private;

    if ($isprivate==TRUE)
    {
        $isprivate="True";
    }
    else
    {
        $isprivate="No";
    }


   
    $isverified=$result->data->user->is_verified;

    if ($isverified==TRUE)
    {
        $isverified="Yes";
    }
    else
    {
        $isverified="No";
    }

    $url=$result->data->user->external_url;
    if ($url=="")
    {
        $url="No URL";
    }

    $biography=$result->data->user->biography;
    if ($biography=="")
    {
        $biography="No Biography";
    }
    $followercount=$result->data->user->edge_followed_by->count;
    $followingcount=$result->data->user->edge_follow->count;
    $totalpost=$result->data->user->edge_owner_to_timeline_media->count;

    $dp_hd=$result->data->user->profile_pic_url_hd;
    $dp=$result->data->user->profile_pic_url;
    echo "Name : <b>". $fullname."</b><br>";
    echo "Username :  <b>"."<a href='https://instagram.com/$username'>$username</a>"." </b><br>";
    echo "BioGraphy : <b>". $biography."</b><br>";
    echo "URL : <b>". "<a href='$url'>$url</a>"."</b><br>";
    echo "Is Verified Account : <b>". $isverified."</b><br>";
    echo "Is Private Account <b>: ". $isprivate."</b><br>";
    echo "Total Posts : <b>". $totalpost."</b><br>";
    echo "Total Followers : <b>". $followercount."</b><br>";
    echo "Total Following : <b>". $followingcount."</b><br>";
    echo "DP : <b><a href='$dp_hd'>High Quality</a> | <a href='$dp'>Normal Quality</a></b><br>";

}
else

{
    echo "Error";
}


?>
  

PHP Instagram Scraper Explanation

In the above code , we are checking the status returned by the json. If it is ok then only means that instagram is returning the informations. You can return only json using this code and then beautify in json beautifier and extract as many information as you want. Make sure to change $user value to the username of which you want to scrape infromation

How To Get Video Posts Count Of Instagram

If you want to get the total number of video and non video posts , then you can use the code given below. Before that , make sure to include error_reporting(0); in your php code as it will show warning and using error_reporting(0); , we can hide the warning.
if ($isprivate=="No")
{


$vidcount=0;
$nonvidcount=0;
for ($i=0;$i <$totalpost;$i++) 
{
    $posts=$result->data->user->edge_owner_to_timeline_media->edges[$i]->node->display_url;
    $vid=$result->data->user->edge_owner_to_timeline_media->edges[$i]->node->video_url;
    if(isset(($vid))) {
        $vidcount+=1;
    }
    else
    {
        $nonvidcount+=1;
    }
    
}

echo "Video : " . $vidcount."<br>";
echo "Non Video : " . $nonvidcount;

}
else
{
    echo "Private Account , Can't Get Video And Non Video Posts Count";
}


In the above output , it returns Video and Non Video post count. My instagram does not contain any video so it returns 0. I Hope now you understand how to extract instagram user detail with php

FAQ

How To Extract Instagram User Detail In PHP

By Using Instagram API , We Can Extract Instagram User Detail

What Are The Valid Headers To Scrape Instagram

For Instagram Scrapping , We Need Valid Headers. In This Post I Have Provided Three Valid Headers Using Which We Can Successfully Scrape Instagram

This Code Will Work On Web Hosting Server

Yes This Code Will Work On Web Hosting. But Instagram Will Apply Rate Limit If You Will Extract Instagram User Information. To Prevent Rate Limiting , You Can Use Proxy Or Wait For Lifting Of Rate Limit