简体   繁体   中英

How to get contents from my Instagram profile?

I have made a script, that reads my Instagram profile information by downloading the pagecontent, and then searching for strings in it. It worked very well, but some months later, the script is sooo slow and results in a white screen. As you can see, I am trying to display 8 values from my Instagram profile. But only if I echo only 2 values at one time, my page loads (but slow) and does not end in a whitescreen of death.

I already tried to display all PHP-Errors, but nothing. All values are correct, if I only display one value.

Is preg_match too slow for 8 values? Are there faster alternatives to do that?

error_reporting(E_ALL);
ini_set('display_errors', 1);

function GetIGInformation($type)
{

    $raw = file_get_contents("https://www.instagram.com/MyUserName");

    if ($type == "1") {
        if (preg_match('/"profile_pic_url_hd":"(.*?)","requested_by_viewer":/', $raw, $ProfilePic2)) {
            return $ProfilePic2[1];
        } else {
            return "https://instagram.fyyz1-1.fna.fbcdn.net/vp/6a15ea059743aabab5376f3d6377a51a/5D5480F1/t51.2885-19/44884218_345707102882519_2446069589734326272_n.jpg?_nc_ht=instagram.fyyz1-1.fna.fbcdn.net";
        }

    }

    if ($type == "2") {
        if (preg_match('/"edge_followed_by":{"count":(.*?)},"followed_by_viewer":/', $raw, $Follower2)) {
            return $Follower2[1];
        } else {
            return "0";
        }
    }

    if ($type == "3") {
        if (preg_match('/"edge_owner_to_timeline_media":{"count":(.*?),"page_info":/', $raw, $Posts2)) {
            return $Posts2[1];
        } else {
            return "No posts";
        }
    }

    if ($type == "4") {
        if (preg_match('/"edge_follow":{"count":(.*?)},"follows_viewer"/', $raw, $Followed2)) {
            return $Followed2[1];
        } else {
            return "Nobody";
        }
    }

    if ($type == "5") {
        if (preg_match('/"external_url":"(.*?)","external_url_linkshimmed":/', $raw, $Website2)) {
            return $Website2[1];
        } else {
            return "No website";
        }
    }

    if ($type == "6") {
        if (preg_match('/"username":"(.*?)","connected_fb_page":/', $raw, $Username2)) {
            return $Username2[1];
        } else {
            return "No username";
        }
    }

    if ($type == "7") {
        if (preg_match('/"display_url":"(.*?)","edge_liked_by":/', $raw, $BilderVorhanden2)) {
            return "Yes";
        } else {
            return "No";
        }
    }

    if ($type == "8") {

        if (preg_match('/"biography":"(.*?)","blocked_by_viewer":/', $raw, $Biographie2)) {
            return $Biographie2[1];
        } else {
            return "No biography";
        }
    }

}

echo GetIGInformation(1);
echo "\n";
echo GetIGInformation(2);
echo "\n";
echo GetIGInformation(3);
echo "\n";
echo GetIGInformation(4);
echo "\n";
echo GetIGInformation(5);
echo "\n";
echo GetIGInformation(6);
echo "\n";
echo GetIGInformation(7);
echo "\n";
echo GetIGInformation(8);
echo "\n";

I found a solution using curl methods:

function curlGetContents($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
    $html = curl_exec($ch);
    $data = curl_exec($ch);
    curl_close($ch);
    return htmlspecialchars($data);
}

$raw = curlGetContents("https://www.instagram.com/MyUserName/");

No, preg_match is not a slow function.

There should be some other issues. I'm not really sure, if this might solve your problem, maybe add memory_limit to -1 such as:

// error_reporting(E_ALL);
error_reporting(0);
ini_set('max_execution_time', 0);
ini_set('memory_limit', '-1');
set_time_limit(0);

function GetIGInformation($type)
{

    $raw = file_get_contents("https://www.instagram.com/MyUserName");

    if ($type == "1") {
        if (preg_match('/"profile_pic_url_hd":"(.*?)","requested_by_viewer":/', $raw, $ProfilePic2)) {
            return $ProfilePic2[1];
        } else {
            return "https://instagram.fyyz1-1.fna.fbcdn.net/vp/6a15ea059743aabab5376f3d6377a51a/5D5480F1/t51.2885-19/44884218_345707102882519_2446069589734326272_n.jpg?_nc_ht=instagram.fyyz1-1.fna.fbcdn.net";
        }

    }

    if ($type == "2") {
        if (preg_match('/"edge_followed_by":{"count":(.*?)},"followed_by_viewer":/', $raw, $Follower2)) {
            return $Follower2[1];
        } else {
            return "0";
        }
    }

    if ($type == "3") {
        if (preg_match('/"edge_owner_to_timeline_media":{"count":(.*?),"page_info":/', $raw, $Posts2)) {
            return $Posts2[1];
        } else {
            return "No posts";
        }
    }

    if ($type == "4") {
        if (preg_match('/"edge_follow":{"count":(.*?)},"follows_viewer"/', $raw, $Followed2)) {
            return $Followed2[1];
        } else {
            return "Nobody";
        }
    }

    if ($type == "5") {
        if (preg_match('/"external_url":"(.*?)","external_url_linkshimmed":/', $raw, $Website2)) {
            return $Website2[1];
        } else {
            return "No website";
        }
    }

    if ($type == "6") {
        if (preg_match('/"username":"(.*?)","connected_fb_page":/', $raw, $Username2)) {
            return $Username2[1];
        } else {
            return "No username";
        }
    }

    if ($type == "7") {
        if (preg_match('/"display_url":"(.*?)","edge_liked_by":/', $raw, $BilderVorhanden2)) {
            return "Yes";
        } else {
            return "No";
        }
    }

    if ($type == "8") {

        if (preg_match('/"biography":"(.*?)","blocked_by_viewer":/', $raw, $Biographie2)) {
            return $Biographie2[1];
        } else {
            return "No biography";
        }
    }

}

echo GetIGInformation(1);
echo "\n";
echo GetIGInformation(2);
echo "\n";
echo GetIGInformation(3);
echo "\n";
echo GetIGInformation(4);
echo "\n";
echo GetIGInformation(5);
echo "\n";
echo GetIGInformation(6);
echo "\n";
echo GetIGInformation(7);
echo "\n";
echo GetIGInformation(8);
echo "\n";

and see what happens.

  • Maybe, some variables/policies may have been changed. You might var_dump(); your variables step by step, to check where the problem might be.
  • You can also set error_reporting(E_ALL); to see, if any warnings/errors might return.

Maybe run this command in your terminal, if using macOS, or find equivalent of the command for other Linux/Windows:

 php -f /path/to/your/php/file.php 

and see what might return.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM