Jump to content


Photo

[RESOLVED] Read html with php


  • Please log in to reply
4 replies to this topic

#1 greenba

greenba
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts
  • LocationBosnia and Herzegovina

Posted 19 September 2006 - 01:50 PM

Hi,

lets say I have an html page at www.domain.com/page.html
what I would like is to get the html code in one string with PHP.

any ideas,

chears

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 19 September 2006 - 02:00 PM

I would use cURL for this task. Make sure you have it installed.

<?php

$url="http://www.domain.com/";

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); 

$html=curl_exec($ch);
curl_close ($ch);

echo htmlentities($html);

?>

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 19 September 2006 - 02:02 PM

There's a function just for this...

It's called file_get_contents

I would use cURL for this task. Make sure you have it installed.


cURL is overkill for what greenba wants.  That is of course if his/her description is correct  :)

Quote from the manual...

file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance.


Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#4 greenba

greenba
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts
  • LocationBosnia and Herzegovina

Posted 19 September 2006 - 02:14 PM

thank you

#5 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 19 September 2006 - 05:07 PM


I would use cURL for this task. Make sure you have it installed.


cURL is overkill for what greenba wants.  That is of course if his/her description is correct  :)

Quote from the manual...

file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance.


Regards
Huggie





I may be wrong- but I think file_get_contents() won't work everytime. I tried to do a simple thing such as:

<?php

echo(file_get_contents("http://www.phpfreaks.com/forums/index.php"));

?>

I get:
Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in /home2/orio/public_html/test.php on line 3

Warning: file_get_contents(http://www.phpfreaks.com/forums/index.php) [function.file-get-contents]: failed to open stream: no suitable wrapper could be found in /home2/orio/public_html/test.php on line 3


But I guess that's only because the php.ini is set that way...
Yep found it:
"allow_url_fopen- Off"

So cURL is the best option for me :)

Orio.
Think you're smarty?

(Gone until 20 to November)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users