PHP Four ways to access URLs
1.fopen mode
Accessing the specified URL function
[PHP]View plain copy print?
- function Access_url ($url) {
- if ($url = =") return false;
- $fp = fopen ($url, ' R ') or exit (' Open url faild! ');
- if ($fp) {
- While (! Feof ($fp)) {
- $file. =fgets ($fp)."";
- }
- Fclose ($fp);
- }
- return $file;
- }
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?
- $content = file_get_contents ("http://www.google.com");
3.curl mode
[PHP]View plain copy print?
- function curl_file_get_contents ($durl) {
- $ch = Curl_init ();
- curl_setopt ($ch, Curlopt_url, $durl);
- curl_setopt ($ch, Curlopt_returntransfer, true); //Get Data back
- curl_setopt ($ch, Curlopt_binarytransfer, true); //Get Data back when Curlopt_returntransfer is enabled
- $r = curl_exec ($ch);
- Curl_close ($ch);
- return $r;
- }
4.fsockopen mode (can only get website homepage information, other pages can not)
[PHP]View plain copy print?
- $fp = fsockopen ("www.example.com", $errno, $errstr, 30);
- if (! $FP) {
- echo "$errstr ($errno) <br/>\n";
- } Else {
- $out ="get/http/1.1\r\n";
- $out. ="host:www.example.com\r\n";
- $out. ="connection:close\r\n\r\n";
- Fwrite ($fp, $out);
- While (! Feof ($fp)) {
- echo fgets ($fp, 128);
- }
- Fclose ($fp);
- }
PHP Four ways to access URLs