php 获取远程文件 保存本地 一般用哪种方法
就是把本地远程的资源 下载到本地
知道的方法有
fopen
file_get_contents
有 curl的 curlopt_file
$fp = fopen($local, w);
curl_setopt($cp, curlopt_file, $fp);
还有socket方法
哪种方法好些??????
就是能够让一些特殊情况下 也能把远程文件保存到本地
比如一个https的资源
$url = https://raw.github.com/robgietema/obviel-bootstrap/18625b502c9a11a90eb18285a2d3cb22c499aa41/libs/jquery/1.8.3/jquery.js;
也能保存到本地
(这是个例子 应该还有其他的情况,只是我不知道举不出例子)
------解决方案--------------------
前两种搭配使用吧
频繁交互长链接可选socket
我怎么对php的socket没啥好感啊
一切以需求为准则
你这个https可这样来处理
$url = https://raw.github.com/robgietema/obviel-bootstrap/18625b502c9a11a90eb18285a2d3cb22c499aa41/libs/jquery/1.8.3/jquery.js;
$ch = curl_init();
curl_setopt($ch, curlopt_url, $url);
curl_setopt($ch, curlopt_header, false);
curl_setopt($ch, curlopt_returntransfer, true);
curl_setopt($ch, curlopt_ssl_verifypeer, false);
curl_setopt($ch, curlopt_useragent, 'mozilla/5.0 (windows nt 6.1) applewebkit/537.11 (khtml, like gecko) chrome/23.0.1271.1 safari/537.11');
$res = curl_exec($ch);
$rescode = curl_getinfo($ch, curlinfo_http_code);
curl_close($ch) ;
file_put_contents(test123.txt,$res);//write
------解决方案--------------------
file_get_contents最方便,但在超时问题上和自身处理错误上有代研究,
curl强大无所不能。只不过需要外加扩展,有的服务器可能不支持,就影响了移植。
至于socket,真没那必要。你真想要用,开源代码有现在的。