AWS Elastic Beanstalk 中的访问被拒绝 php curl


Access denied php curl in AWS Elastic Beanstalk

我有一个PHP应用程序,它使用curl来访问外部URL并分析页面的内容。我的代码在本地主机上运行得很好,但是当我让它在服务器上运行时会显示错误:

访问被拒绝

您无权访问此"http://www.example.com" 服务器。参考 #18.aec67bc8.1417892829.438558f

我的代码

$cookie_file = '';
$post_fields = 'id=1';
$agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com');
curl_setopt($ch, CURLOPT_USERAGENT,$agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);     
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);    
$output = curl_exec($ch);

知道吗?

EC2的IP范围经常被阻止抓取某些网站,包括StackOverflow。

代码对我有用。您可能需要在 AWS 实例上打开端口 80。您可以通过编辑与 Elastic Beanstalk 创建的实例关联的安全组来执行此操作。