【发布时间】:2020-07-07 22:28:53
【问题描述】:
我正在使用此代码从 Instagram 帐户获取图片:
<?php
$response = file_get_contents('https://www.instagram.com/fcbarcelona/?__a=1');
$response = json_decode($response, TRUE);
foreach($response['graphql']['user']['edge_owner_to_timeline_media']['edges'] as $img) {
?>
<div class="sbi_item sbi_type_image sbi_new sbi_transition" id="sbi_1231046275994525759_3147458623" data-date="1460972173">
<div class="sbi_photo_wrap">
<img src="<?php echo $img['node']['display_url']; ?>">
</div>
</div>
<?php
}
?>
在我的 Wamp 服务器上一切正常!但是当我从TransIp 将代码上传到我的网站服务器时,我无法从 Instagram 接收 JSON。
我的网站服务器可能不支持 file_get_contents 吗?我怎样才能检查它是?还有其他方法可以检索 JSON 吗?
卷曲方式:
$url = "https://www.instagram.com/fcbarcelona/?__a=1";
// Initiate curl
$ch = curl_init();
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
// Will dump a beauty json :3
var_dump(json_decode($result, true));
【问题讨论】:
-
"Is file_get_contents maybe not supported on my website server?"- 这肯定是托管技术支持的问题吗?您是否尝试过使用cURL在您的直播主机上获取请求? -
@ProfessorAbronsius cURL 不工作.....见上面的代码
-
在处理 SSL 连接时,您通常需要采取额外的步骤来处理 SSL 协商。
file_get_contents将接受context参数,您可以对其进行相应配置。 cURL 也是如此 - -
@ProfessorAbronsius 你有例子吗?上面我在堆栈溢出时发现的 cURL 代码 (stackoverflow.com/questions/16700960/…)