Home > Backend Development > PHP Tutorial > Summary of Several Methods to Open URL Address in PHP_PHP Tutorial

Summary of Several Methods to Open URL Address in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:37:35
Original
826 people have browsed it

1: Use file_get_contents to get the content in get mode

Copy the code The code is as follows:

$url='http://www.baidu.com/';
$html = file_get_contents($url);
//print_r($http_response_header);
ec($html );
printhr();
printarr($http_response_header);
printhr();
?>

Sample code 2: Open with fopen url, get the content by get method
Copy code The code is as follows:

$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 in post mode
Copy code The code is as follows:

$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('http://localhost/e/admin/test.html', false, $context);
echo $html;
?>

Sample code 4: Use the fsockopen function to open the url and obtain the complete data in get mode, including header and body
Copy code The code is as follows:

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: $cookien" ;
$request.="";
fwrite($fp,$request);
while(!@feof($fp)) {
$result .= @fgets($fp, 1024);
}
fclose($fp);
return $result;
}
}
//Get the html part of the url, remove the 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;
}

?> ;

Sample code 5: Use the fsockopen function to open the url and obtain the complete data in POST mode, including header and body
Copy the code The code is as follows:

function HTTP_Post($URL,$data,$cookie, $referrer="") {
// parsing the given URL
$URL_Info=parse_url($URL);
// Building referrer
if($referrer=="") // if not given use this script as referrer
$referrer ="111";
// making string from $data
foreach($data as $key=>$value)
$values[]="$key=".urlencode($value) ;
$data_string=implode("&",$values);
// Find out which port is needed - if not given use standard (=80)
if(!isset($URL_Info[" port"]))
$URL_Info["port"]=80;
// building POST-request:
$request.="POST ".$URL_Info["path"]." HTTP/ 1.1n";
$request.="Host: ".$URL_Info["host"]."n";
$request.="Referer: $referern";
$request.=" Content-type: application/x-www-form-urlencodedn";
$request.="Content-length: ".strlen($data_string)."n";
$request.="Connection: closen ";
$request.="Cookie: $cookien";
$request.="n";
$request.=$data_string."n";
$fp = fsockopen($ URL_Info["host"],$URL_Info["port"]);
fputs($fp, $request);
while(!feof($fp)) {
$result .= fgets( $fp, 1024);
}
fclose($fp);
return $result;
}
printhr();
?>

Sample code 6: Use the curl library. Before using the curl library, you may need to check php.ini to see if the curl extension has been turned on
Copy Code The code is as follows:

$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, 'http://www.baidu.com/') ;
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
?>

About curl library:
curl official website http://curl.haxx.se/
curl uses URL syntax File transfer tool supports FTP, FTPS, HTTP HTPPS SCP SFTP TFTP TELNET DICT FILE and LDAP. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploads, Kerberos, HTTP-based uploads, proxies, cookies, user + password proof, file transfer recovery, http proxy channels and a lot of other useful tricks
Copy code The code is as follows:

function printarr(array $arr)
{
echo "< br> Row field count: ".count($arr)."
";
foreach($arr as $key=>$value)
{
echo "$key=$ value
";
}
}
?>

7.
Some hosting service providers turn off the allow_url_fopen option of php , that is, you cannot directly use file_get_contents to obtain the content of the remote web page. That is, you can use another function curl.
The following are different ways of writing the same function of file_get_contents and curl
Example of using file_get_contents function:
Copy code The code is as follows :

< ?php
$file_contents = file_get_contents('http://www.ccvita.com/');
echo $file_contents;
?>

Example of using curl function:
Copy code The code is as follows:

< ?php
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, 'http://www.ccvita.com');
curl_setopt ($ch , CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
?>

Use the function_exists function to determine whether php supports a function. You can easily write the following function
Copy code The code is as follows:

< ?php
function vita_get_url_content($url) {
if(function_exists('file_get_contents')) {
$file_contents = file_get_contents($url);
} else {
$ch = curl_init();
$timeout = 5;
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);
}
return $file_contents;
}
?>

In fact, the above function is still open to question. If your hosting service provider turns off both file_get_contents and curl, the above function will An error occurred.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321918.htmlTechArticle1: Use file_get_contents to get the content in get mode. Copy the code as follows: ?php $url='http:// www.baidu.com/'; $html = file_get_contents($url); //print_r($http_response_header); ec...
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