浏览器打开一个链接成功,但不卷曲和的file_get_contents

问题描述 投票:0回答:1

我试图使用Instagram的API。当我打开下面的浏览器链接,这是完全没问题的,你可以点击它,看看JSON响应:https://www.instagram.com/nasa/?__a=1

当我试图通过file_get_contents()打开相同的URL我面对403 Forbidden错误。

所以我试图用卷曲。这里是我的代码:

$url = "https://www.instagram.com/nasa/?__a=1";
$agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);


var_dump($result);

问题是$result是一个空字符串。当我尝试使用的file_get_contents内容得到,我面对403禁止错误,当我试图让内容使用curl它返回一个空字符串。

有些机构可以帮助? TNX。

编辑 我不得到403禁止在我的浏览器,因为我登录。

php curl file-get-contents
1个回答
1
投票

您需要启用cookie支持(如CURLOPT_COOKIEFILE)并登录,然后才能访问https://www.instagram.com/nasa/?__a=1,和你的卷曲的代码不会尝试登录。

在这里你可以看到如何登录到Instagram的用PHP:https://stackoverflow.com/a/41684531/1067003

© www.soinside.com 2019 - 2024. All rights reserved.