Home > Backend Development > PHP Tutorial > What are the methods to request url in php

What are the methods to request url in php

零下一度
Release: 2023-03-14 08:02:01
Original
3849 people have browsed it

A summary of several methods of opening URL addresses in PHP. The functions here are mainly used for functions such as thief collection.

1: Use file_get_contents

Get the content in get mode

<?php 
$url=&#39;www.baidu.com/&#39;; 
$html = file_get_contents($url); 
//print_r($http_response_header); 
ec($html); 
printhr(); 
printarr($http_response_header); 
 
printhr(); 
?>
Copy after login

Sample code 2: Use fopen to open the url,

Get the content by get method

<? 
$fp = fopen($url, &#39;r&#39;); 
 
printarr(stream_get_meta_data($fp)); 
printhr(); 
while(!feof($fp)) { 
 
$result .= fgets($fp, 1024); 
} 
echo "url body: $result"; 
 
printhr(); 
fclose($fp); 
?>
Copy after login

Sample code 3: Use the file_get_contents function to get the url by post method

<?php 
$data = array (&#39;foo&#39; => 
&#39;bar&#39;); 
$data = http_build_query($data); 
$opts = array ( 
&#39;http&#39; 
=> array ( 
&#39;method&#39; => &#39;POST&#39;, 
&#39;header&#39;=> "Content-type: 
application/x-www-form-urlencoded" . 
"Content-Length: " . strlen($data) . 
"", 
&#39;content&#39; => $data 
), 
); 
$context = 
stream_context_create($opts); 
$html = 
file_get_contents(&#39;localhost/e/admin/test.html&#39;, false, $context); 
 
echo $html; 
?>
Copy after login

Sample code 4: Use the fsockopen function to open the url, and get the complete data by get method, including header and body

<? 
function get_url 
($url,$cookie=false) { 
$url = parse_url($url); 
$query = 
$url[path]."?".$url[query]; 
ec("Query:".$query); 
$fp = fsockopen( 
$url[host], $url[port]?$url[port]:80 , $errno, $errstr, 30); 
if (!$fp) { 
 
return false; 
} else { 
$request = "GET $query HTTP/1.1"; 
 
$request .= "Host: $url[host]"; 
$request .= "Connection: Close"; 
 
if($cookie) $request.="Cookie: $cookie\n"; 
$request.=""; 
 
fwrite($fp,$request); 
while(!@feof($fp)) { 
$result .= @fgets($fp, 
1024); 
} 
fclose($fp); 
return $result; 
} 
} 
//获取url的html部分,去掉header 
function GetUrlHTML($url,$cookie=false) { 
 
$rowdata = get_url($url,$cookie); 
if($rowdata) 
{ 
$body= 
stristr($rowdata,""); 
$body=substr($body,4,strlen($body)); 
return $body; 
} 
return false; 
} 
?>
Copy after login

I encountered a problem recently during development. Line 4 of the program will request a URL. By searching for relevant information, I found that there are many methods. This article introduces you to five ways to request URLs in PHP. Three methods are to use fopen() function, file() function, file_get_contents() function, curl() to request remote url data and exec() to execute command line commands

This article mainly I will introduce you to the five methods of requesting URLs in PHP and share them for your reference and study. Without further ado, let’s take a look at the detailed introduction:

Five methods :

  • The first three are the basic file operation functions of PHP

  • curl()It is the php extension that needs to be enabled, and it needs to be installed under Linux

  • exec()The command wget download under the Linux command line is executedRemote file

The wget command failed when testing the local virtual machine to request www.baidu.com, but it worked on the remote server. DNS resolution was considered. problem, so I directly requested the IP and successfully downloaded the index.html file.

Only methods are provided here. The advantages and disadvantages require a detailed understanding of the functions and defects of each method.

1. fopen() function

$file = fopen("www.jb51.net", "r") or die("打开远程文件失败!");
while (!feof($file)) {
 $line = fgets($file, 1024);
 //使用正则匹配标题标记
 if (preg_match("/<title>(.*)<\/title>/i", $line, $out)) { 
 $title = $out[1]; //将标题标记中的标题字符取出
 break; //退出循环,结束远程文件读取
 }
}
fclose($file);
Copy after login

2. file() function


$lines = file("www.jb51.net/article/48866.htm");
readfile(www.jb51.net/article/48866.htm);
Copy after login

3. file_get_contents() function

##

$content = file_get_contents(www.jb51.net/article/48866.htm);
Copy after login

4. curl() requests remote url data

$url = "www.baidu.com";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$contents = curl_exec($ch);
curl_close($ch);
Copy after login

5. exec() executes the command line Command

//exec("wget 220.181.111.188");
shell_exec("wget 220.181.111.188");
Copy after login

The above is the detailed content of What are the methods to request url in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template