有没有办法在PHP5中使用没有cUrl的自定义用户代理从远程服务器下载文件

时间:2016-10-16 17:46:39

标签: php curl

现在我有了这个(它可以在我的localhost上运行):

function get_data($url) {
$ch = curl_init();
$timeout = 30;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt ($ch, CURLOPT_CAINFO, getcwd()."/cacert.pem");
$ua = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.16 (KHTML, like Gecko) \ Chrome/24.0.1304.0 Safari/537.16';
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
$data = curl_exec($ch); 
curl_close($ch);
return (string)$data;

}

但是我的网络服务器不允许我使用cUrl。我如何在php中重写这个代码,它不会使用cUrl?

提前致谢

1 个答案:

答案 0 :(得分:0)

试试这个

<?php
 $opt = array(
    'http'=>array(
    'method'=>"GET",
    'header'=>"User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.16 (K ..."
   "ssl"=>array(
    "cafile" => "/path-to-cacert/cacert.pem",
    "verify_peer"=> true,
    "verify_peer_name"=> true,
),
 )
);


$file = file_get_contents('https://www.example.com/', false, stream_context_create($opts));