How to send GET request using PHP

autoload
Release: 2023-03-09 09:46:01
Original
12316 people have browsed it

How to send GET request using PHP


##Method 1: Use fopen() to open the url

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

Method 2: Use file_get_contents()

<?php 
    $url=&#39;https://www.adminn.cn/&#39;; 
    $html = file_get_contents($url);
    echo $html; 
?>
Copy after login

Method 3: Use the fsockopen() function to open the url, fsockopen( ) function requires the allow_url_fopen option in PHP.ini to be turned on.

<?php 
function get_url ($url,$cookie=false) 
{ 
    $url = parse_url($url); 
    $query = $url[path].”?”.$url[query]; 
    echo “Query:”.$query; 
    $fp = fsockopen( $url[host], $url[port]?$url[port]:80 , $errno, $errstr, 30); 
    if (!$fp) 
    { 
        return false; 
    } else { 
        $request = “GET $query HTTP/1.1rn”;
         $request .= “Host: $url[host]rn”; 
         $request .= “Connection: Closern”; 
         if($cookie) $request.=”Cookie:  $cookien”; 
         $request.=”rn”; 
         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,”rnrn”); 
      $body=substr($body,4,strlen($body)); 
      return $body; 
   } 
   return false; 
 } 
?>
Copy after login

Recommended: 2021 PHP interview questions summary (collection)》《php video tutorial

The above is the detailed content of How to send GET request using 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