Solved

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

Posted on 2004-09-08
12
230 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

710 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