PHP collection remote server content code

Source: Internet
Author: User

Method simulates a user's access to a Web page

The code is as follows Copy Code
function Readpr ($link, $url)
{
$fp = Fsockopen ($url, $errno, $errstr, 30);
if (! $fp)
{
echo "$errstr ($errno)";
Exit (1);
}
Else
{
$out = "Get $link http/1.0";
$out. = "Host: $url";
$out. = "user-agent:mozilla/4.0 (compatible; Googletoolbar 2.0.114.9-big; Linux 2.6) ";
$out. = "Connection:close";
Fwrite ($fp, $out);
do{
$line = fgets ($fp, 128);
}while ($line!== "");
$data = Fread ($fp, 8192);
Fclose ($FP);
return $data;
}
}

Method two uses Curl_init to read the content of the Remote Web page

  code is as follows copy code

function Init ()
{
   $ch = Curl_init ();
  curl_setopt ($ch, Curlopt_url, $url);
   curl_setopt ($ch, Curlopt_returntransfer, 1);
  curl_setopt ($ch, Curlopt_connecttimeout, $timeout);
   $file _contents = curl_exec ($ch);
  curl_close ($ch);
}
//Method Three simplest file_get_contents

Function GetFiles ($value)
{
  $get _file = @file_get_contents ($value);
}
//method four use fopen to collect Remote Web page content

Function GetFiles ($value)
{
 return fopen ($value);
}

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.