How to get the HTML code of a web page in PHP

3 Answers

0 votes
function get_html($url) {
    $handle = curl_init();
          
    curl_setopt($handle, CURLOPT_HTTPGET, true);
    curl_setopt($handle, CURLOPT_HEADER, true);
    curl_setopt($handle, CURLOPT_URL, $url);
    curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
          
    $output = curl_exec($handle);
  
    curl_close($handle);
          
    $separator = "\r\n\r\n";
    $header = substr($output, 0, strpos($output, $separator));
     
    $body_start = strlen($header) + strlen($separator);
    $html = substr($output, $body_start, strlen($output) - $body_start);
      
    return $html;
}
  
  
$url = "https://seek4info.com";
  
$html = get_html($url);
  
echo $html;
  
  
/*
run:
  
<!doctype html>
<html>
    <head>
        <title>Seek.4Info - Relevant Search Engine</title>        
        <meta http-equiv="Content-type" content="text/html; charset=utf-8">
        <meta name="keywords" content="...
        ...
  
*/

 



answered Sep 13, 2019 by avibootz
edited Sep 13, 2019 by avibootz
0 votes
$url = "https://seek4info.com";
  
$html = file_get_contents($url);
  
echo $html;
  
  
/*
run:
  
<!doctype html>
<html>
    <head>
        <title>Seek.4Info - Relevant Search Engine</title>        
        <meta http-equiv="Content-type" content="text/html; charset=utf-8">
        <meta name="keywords" content="...
        ...
  
*/

 



answered Sep 13, 2019 by avibootz
0 votes
function get_html($url) {
    $ci = curl_init($url);
    
    curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
    
    $html = curl_exec($ci);

    if (curl_error($ci)) {
        die(curl_error($ci));
    }

    curl_close($ci);
      
    return $html;
}
  
  
$url = "https://seek4info.com";
  
$html = get_html($url);
  
echo $html;
  
  
/*
run:
  
<!doctype html>
<html>
    <head>
        <title>Seek.4Info - Relevant Search Engine</title>        
        <meta http-equiv="Content-type" content="text/html; charset=utf-8">
        <meta name="keywords" content="...
        ...
  
*/

 



answered Sep 13, 2019 by avibootz

Related questions

2 answers 1,034 views
1 answer 217 views
1 answer 188 views
2 answers 246 views
1 answer 260 views
...