PHP Four ways to access URLs

Source: Internet
Author: User

PHP Four ways to access URLs

1.fopen mode
Accessing the specified URL function

[PHP]View plain copy print?
  1. function Access_url ($url) {
  2. if ($url = =") return false;
  3. $fp = fopen ($url, ' R ') or exit (' Open url faild! ');
  4. if ($fp) {
  5. While (! Feof ($fp)) {
  6. $file. =fgets ($fp)."";
  7. }
  8. Fclose ($fp);
  9. }
  10. return $file;
  11. }


The 2.file_get_contents method (which causes the CPU to soar when a remote file is opened). File_get_contents can actually post)

[PHP]View plain copy print?
    1. $content = file_get_contents ("http://www.google.com");


3.curl mode

[PHP]View plain copy print?
  1. function curl_file_get_contents ($durl) {
  2. $ch = Curl_init ();
  3. curl_setopt ($ch, Curlopt_url, $durl);
  4. curl_setopt ($ch, Curlopt_returntransfer, true); //Get Data back
  5. curl_setopt ($ch, Curlopt_binarytransfer, true); //Get Data back when Curlopt_returntransfer is enabled
  6. $r = curl_exec ($ch);
  7. Curl_close ($ch);
  8. return $r;
  9. }


4.fsockopen mode (can only get website homepage information, other pages can not)

[PHP]View plain copy print?
    1. $fp = fsockopen ("www.example.com", $errno, $errstr, 30);
    2. if (! $FP) {
    3. echo "$errstr ($errno) <br/>\n";
    4. } Else {
    5. $out ="get/http/1.1\r\n";
    6. $out. ="host:www.example.com\r\n";
    7. $out. ="connection:close\r\n\r\n";
    8. Fwrite ($fp, $out);
    9. While (! Feof ($fp)) {
    10. echo fgets ($fp, 128);
    11. }
    12. Fclose ($fp);
    13. }

PHP Four ways to access URLs

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.