Which method does php use to obtain remote files and save them locally?

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.