Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hello,
I am a beginner in PHP programming and I've been having a particular problem with reading the contents of a url. Let's take for example the search results page from google for any random keyword.
[url]http://www.google.com/search?hl=en&ie=ISO-8859-1&q=phpdn&btnG=Google+Search[/url]
This is the link for the search results pages for the keyword "phpdn" . When you visit this url with your browser and do a View Document Source you will see that there are various HTML comment tags (i.e. <!--a-->, <!--m-->) in the HTML code. As a first step I try to read the entire page using a simple PHP program. I have tried three different ways to do that:Code: Select all
<?php
$website = "http://www.google.com/search?hl=en&ie=ISO-8859-1&q=phpdn&btnG=Google+Search";
1. Using HTTP_Request from PEAR:
require 'HTTP/Request.php';
$r = new HTTP_Request($website);
$r->sendRequest();
$page = $r->getResponseBody();
2. Using the cURL extension:
$c = curl_init($website);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($c);
curl_close($c);
3. Using fopen and fread:
$page = '';
$fh = fopen($website, 'r') or die($php_errormsg);
while (! feof($fh)) {
$page .= fread($fh,1048576);
}
fclose($fh);
At the end I print $page:
echo $page;
?>Thank you,
Nikolaos
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]