如何从我的Instagram个人资料中获取内容?

时间:2019-04-25 01:10:02

标签: php regex

我制作了一个脚本,该脚本通过下载pagecontent并在其中搜索字符串来读取我的Instagram个人资料信息。 它工作得很好,但是几个月后,脚本太慢了,并导致白屏。如您所见,我正在尝试显示我的Instagram个人资料中的8个值。但是,仅当我一次仅回显2个值时,我的页面才会加载(但速度很慢),并且不会以白屏死光结束。

我已经尝试显示所有PHP错误,但是什么也没有。如果我只显示一个值,则所有值都是正确的。

preg_match对于8个值是否太慢?有更快的替代方法吗?

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

2 个答案:

答案 0 :(得分:2)

我找到了使用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/");

答案 1 :(得分:0)

  

否,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 

,看看可能会返回什么。