Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-09-08
12
Medium Priority
?
251 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 400 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.
Suggested Courses

886 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