PHP 4 ways to read remote files

Source: Internet
Author: User

1. fopen, Fread
1if ($file = fopen ("http://www.example.com/", "R")) {
2 while (!feof ($file))
3 $data. = Fread ($file, 1024);
4}
5fclose ($file);
2. file_get_contents
In a very simple sentence:
$data = file_get_contents ("http://www.example.com/");
If you want to limit the time-out, you need to use its $context parameter
1$opts = Array (' http ' = = Array (' timeout ' = 30));
2$context = Stream_context_create ($opts);
3$data = file_get_contents ("http://www.example.com/", false, $context);
Where the second parameter, $use_include_path, means finding the file in the include_path of the php.ini setting, using false.
In addition, this function can also send post data:
1$opts = Array (' http ' = = Array (
2 ' method ' = ' POST ',
3 ' content ' = ' x=1&y=2 ');
4$context = Stream_context_create ($opts);
5$data = file_get_contents ("http://www.example.com/", false, $context);
The second method is relatively quick. The above two methods require php.ini setup allow_url_fopen=on.
3. Fsockopen, Fwrite, fread
01if ($fp = fsockopen (' www.example.com ', $errno, $errstr, 30)) {
$header = "get/ip.php?ip= $ip http/1.0rn";
$header. = "HOST:WWW.EXAMPLE.COMRN";
$header. = "Connection:closernrn";
Fwrite ($fp, $header);
Stream_set_timeout ($FP, 2);
(!feof ($FP))
$data. = Fread ($fp, 128);
Fclose ($FP);
10}
This method requires the Php_sockets extension to be turned on
4. Curl
1$curl = Curl_init ();
2curl_setopt ($curl, Curlopt_url, "http://www.example.com/");
3curl_setopt ($curl, Curlopt_returntransfer, 1);
4curl_setopt ($curl, Curlopt_timeout, 30);
5$data = curl_exec ($curl);
6curl_close ($curl);
Curl can also be used to send post data and send HTTP request header information, the following is another use case:
01$curl = Curl_init ();
02curl_setopt_array ($curl, Array (
Curlopt_url = "Http://192.168.1.200/",
Curlopt_returntransfer = 1,
Curlopt_postfields + = Array (' name ' = ' = ' Foo ', ' password ' = ' Bar '),
Curlopt_post = 1,
Curlopt_httpheader + = Array (' Host:www.example.com ', ' Referer:www.example.com '),
08);
09$data = curl_exec ($curl);
10curl_close ($curl);
This method requires the Php_curl extension to be turned on

PHP 4 ways to read remote files

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.