Solved

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

Posted on 2004-09-08
12
180 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:
Squinky 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 25

Expert Comment

by:Squinky
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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 create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now