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
  • 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

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 (iPhone11,8; iOS 12_3_1; en_US; en-US; scale=2.00; 828x1792; 165586599)";
$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 Android (28/9; 420dpi; 1080x2260; OnePlus; GM1903; OnePlus7; qcom; sv_SE; 164094539)";
$header="User-Agent':'Instagram 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.

$header="User-Agent':'Instagram 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, '');
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);

if ($status=="ok")

    if ($isprivate==TRUE)


    if ($isverified==TRUE)

    if ($url=="")
        $url="No URL";

    if ($biography=="")
        $biography="No Biography";

    echo "Name : <b>". $fullname."</b><br>";
    echo "Username :  <b>"."<a href='$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>";


    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")

for ($i=0;$i <$totalpost;$i++) 
    if(isset(($vid))) {

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

    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


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