首页 > 解决方案 > 亚马逊 AWS 拒绝 cURL 访问

问题描述

尝试使用 cURL 下载网络摄像头图像。

以下返回访问被拒绝:

<?php

$url = 'http://s3-us-west-2.amazonaws.com/alertwildfire-data-public/Axis-CupertinoHills/latest_full.jpg';

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);

header('Content-type: image/jpeg');
echo $data;

URL 来自此链接:

http://www.alertwildfire.org/index.html

选择区域然后查看网络摄像头并右键单击以查看图像。

标签: phpcurl

解决方案


您需要设置适当的“Referer”标头

例子:

curl_setopt($ch, CURLOPT_REFERER, "http://www.alertwildfire.org/blmnv/index.html?camera=Axis-Angel&v=81e002f");

或者

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Referer: http://www.alertwildfire.org/blmnv/index.html?camera=Axis-Angel&v=81e002f'
));

推荐阅读