[英]How to get contents from my Instagram profile?
我編寫了一個腳本,該腳本通過下載pagecontent並在其中搜索字符串來讀取我的Instagram個人資料信息。 它工作得很好,但是幾個月后,腳本太慢了,並導致白屏。 如您所見,我正在嘗試顯示我的Instagram個人資料中的8個值。 但是,僅當我一次僅回顯2個值時,我的頁面才會加載(但速度很慢),並且不會以死亡白屏告終。
我已經嘗試顯示所有PHP錯誤,但是什么也沒有。 如果我只顯示一個值,則所有值都是正確的。
對於8個值, preg_match
是否太慢? 有更快的替代方法嗎?
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";
我找到了使用curl方法的解決方案:
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/");
不,
preg_match
不是一個慢函數。
應該還有其他一些問題。 我不確定,如果這可以解決您的問題,可以將memory_limit
添加到-1
例如:
// 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";
看看會發生什么。
var_dump();
逐步檢查您的變量,以檢查問題所在。 error_reporting(E_ALL);
查看是否可能返回任何警告/錯誤。 如果使用macOS,則可能在終端中運行此命令,或者找到與其他Linux / Windows等效的命令:
php -f /path/to/your/php/file.php
看看會返回什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.