Solved

Any function for getting the html page source into a srting?

Posted on 2004-09-08
12
207 Views
Last Modified: 2008-03-17
Can I ask how can I get the html page source of a specific URL into a string. Any PHP class / function can do that??

Thanks
0
Comment
Question by:ping1234
  • 2
  • 2
  • 2
  • +3
12 Comments
 
LVL 33

Expert Comment

by:sajuks
ID: 12003922
0
 
LVL 25

Accepted Solution

by:
Marcus Bointon earned 100 total points
ID: 12003930
implode??

I think what you're after is:

$html = file_get_contents($url);
0
 
LVL 33

Expert Comment

by:sajuks
ID: 12003965
Was using cut and paste from the link and did the wroing cut. Squinky you are rite. The link covers that too.
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 9

Expert Comment

by:keteracel
ID: 12005221
Only problem with file_get_contents() is that servers may think that a spider is accessing the page and so can send a different page back than the one you were expecting! (google does this!)

To combat this, use the following function:

function getFileSource($host, $file) {
   $fp = fsockopen($host, 80, $errno, $errstr, 30);
   $file = "";

   if (!$fp) {
      echo "$errstr ($errno)<br />\n";
   } else {
      $out = "GET /$file HTTP/1.1\r\n";
      $out .= "Host: $host\r\n";
      $out .= "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20030225\r\n";
      $out .= "Connection: Close\r\n\r\n";

      fwrite($fp, $out);
     
      while (!feof($fp)) {
          $file .= fgets($fp, 128);
      }
      fclose($fp);
   }
   return $file;
}
0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 12005457
If it's really a problem, it's better to use CURL, as you also get free features like proxy support, cookies and redirect following. From CURL user notes at http://www.php.net/manual/en/ref.curl.php:

function curl_string ($url,$user_agent,$proxy){

       $ch = curl_init();
       curl_setopt ($ch, CURLOPT_PROXY, $proxy);
       curl_setopt ($ch, CURLOPT_URL, $url);
       curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
       curl_setopt ($ch, CURLOPT_COOKIEJAR, "c:\cookie.txt");
       curl_setopt ($ch, CURLOPT_HEADER, 1);
       curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
       curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
       $result = curl_exec ($ch);
       curl_close($ch);
       return $result;
}

$url_page = "http://www.google.com/";
$user_agent = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20030225";
$proxy = "http://208.25.243.167:8080";
$string = curl_string($url_page,$user_agent,$proxy);
echo $string;
0
 
LVL 10

Expert Comment

by:ThaSmartUno
ID: 12006147
does $result = curl_exec ($ch); actually work? ... it doesnt for me i have to do output buffering etc.
0
 
LVL 4

Expert Comment

by:iceboxman
ID: 12007404
It does if curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); is set
0
 
LVL 10

Expert Comment

by:ThaSmartUno
ID: 12007992
i see thanks
0
 
LVL 5

Expert Comment

by:Daij-Djan
ID: 12033677
file_get_contents !?
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question