Which of the following methods can be used for php to obtain remote files and store them locally? download the local remote resources to the local machine. the methods available are: & nbsp; fopenfile_get_contents & nbsp; curl & nbsp; CURLOPT_FILE $ fp & nbsp; = & php which method is generally used to obtain remote files and save them locally?
Is to download local remote resources to the local
The methods are as follows:
Fopen
File_get_contents
CURLOPT_FILE with curl
$ Fp = fopen ($ local, "w ");
Curl_setopt ($ cp, CURLOPT_FILE, $ fp );
There are also socket methods
Which method is better ??????
In some special cases, remote files can be stored locally.
For example, an https resource
$ Url = "https://raw.github.com/robgietema/obviel-bootstrap/18625b502c9a11a90eb18285a2d3cb22c499aa41/libs/jquery/1.8.3/jquery.js ";
It can also be saved locally.
(This is an example. there are other cases, but I don't know)
------ Solution --------------------
Use the first two methods.
Optional socket for frequent interaction and persistent connections
Why am I not so fond of php socket?
Everything is based on requirements
You can handle this 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
------ Solution --------------------
File_get_contents is the most convenient, but there have been research on timeout issues and troubleshooting errors,
Curl is powerful and omnipotent. Some servers may not support expansion, which affects migration.
As for socket, it is unnecessary. You really want to use it. the open source code has the current one.