Home > Article > Backend Development > What are the methods to request url in php
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='www.baidu.com/'; $html = file_get_contents($url); //print_r($http_response_header); ec($html); printhr(); printarr($http_response_header); printhr(); ?>
Sample code 2: Use fopen to open the url,
Get the content by get method
<? $fp = fopen($url, 'r'); printarr(stream_get_meta_data($fp)); printhr(); while(!feof($fp)) { $result .= fgets($fp, 1024); } echo "url body: $result"; printhr(); fclose($fp); ?>
Sample code 3: Use the file_get_contents function to get the url by post method
<?php $data = array ('foo' => 'bar'); $data = http_build_query($data); $opts = array ( 'http' => array ( 'method' => 'POST', 'header'=> "Content-type: application/x-www-form-urlencoded" . "Content-Length: " . strlen($data) . "", 'content' => $data ), ); $context = stream_context_create($opts); $html = file_get_contents('localhost/e/admin/test.html', false, $context); echo $html; ?>
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; } ?>
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 executed
Remote file
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);
2. file() function
$lines = file("www.jb51.net/article/48866.htm"); readfile(www.jb51.net/article/48866.htm);
3. file_get_contents() function
##
$content = file_get_contents(www.jb51.net/article/48866.htm);
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);
5. exec() executes the command line Command//exec("wget 220.181.111.188");
shell_exec("wget 220.181.111.188");
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!