Problems with reading content from the web using fopen()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sandrol76
Forum Newbie
Posts: 8
Joined: Mon Mar 01, 2004 3:25 am

Problems with reading content from the web using fopen()

Post by sandrol76 »

Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hi all,
I used the following code to read the html response from a website but I can't understand why it doesn't work. Here is the code:

Code: Select all

$filename = "http://www.livescore.com/";
$handle = fopen($filename, "r");
$HTML = "";
while ($line = fgets ( $handle , 256 )) {
     $HTML.= $line;
}
echo $HTML;

fclose($handle);
It does work with other web sites but not with that one. Can you help me with that?
Thank you in advance.


Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

that host does not allows remote file opens with fopen based functions

try something along the lines of

Code: Select all

<?php
/*
   * @return string
   * @param string $url
   * @desc Return string content from a remote file
   * @author Luiz Miguel Axcar (lmaxcar@yahoo.com.br)
*/

function get_content($url)
{
   $ch = curl_init();

   curl_setopt ($ch, CURLOPT_URL, $url);
   curl_setopt ($ch, CURLOPT_HEADER, 0);

   ob_start();

   curl_exec ($ch);
   curl_close ($ch);
   $string = ob_get_contents();

   ob_end_clean();
  
   return $string;   
}

#usage:
$content = get_content ("http://www.php.net");
var_dump ($content);
?>
sandrol76
Forum Newbie
Posts: 8
Joined: Mon Mar 01, 2004 3:25 am

Post by sandrol76 »

I tried that but I got the following error:
Fatal error: Call to undefined function: curl_init() on line 4

Any suggestion?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

you have to have the cURL library installed on your server.. get it here

Installation
Installation

To use PHP's CURL support you must also compile PHP --with-curl[=DIR] where DIR is the location of the directory containing the lib and include directories. In the "include" directory there should be a folder named "curl" which should contain the easy.h and curl.h files. There should be a file named libcurl.a located in the "lib" directory. Beginning with PHP 4.3.0 you can configure PHP to use CURL for URL streams --with-curlwrappers.

Note to Win32 Users: In order to enable this module on a Windows environment, you must copy libeay32.dll and ssleay32.dll from the DLL folder of the PHP/Win32 binary package to the SYSTEM folder of your Windows machine. (Ex: C:\WINNT\SYSTEM32 or C:\WINDOWS\SYSTEM)
sandrol76
Forum Newbie
Posts: 8
Joined: Mon Mar 01, 2004 3:25 am

Post by sandrol76 »

OK, thank you very much indeed for the help and for replying so quickly!
SugahPie
Forum Newbie
Posts: 1
Joined: Mon Nov 07, 2005 12:37 pm

Post by SugahPie »

Jcart wrote:you have to have the cURL library installed on your server.. get it here

Installation
Installation

To use PHP's CURL support you must also compile PHP --with-curl[=DIR] where DIR is the location of the directory containing the lib and include directories. In the "include" directory there should be a folder named "curl" which should contain the easy.h and curl.h files. There should be a file named libcurl.a located in the "lib" directory. Beginning with PHP 4.3.0 you can configure PHP to use CURL for URL streams --with-curlwrappers.

Note to Win32 Users: In order to enable this module on a Windows environment, you must copy libeay32.dll and ssleay32.dll from the DLL folder of the PHP/Win32 binary package to the SYSTEM folder of your Windows machine. (Ex: C:\WINNT\SYSTEM32 or C:\WINDOWS\SYSTEM)
I am trying to install curl, but I have no clue what I'm doing and I can't find any specific instructions. What does "you must also compile PHP --with-curl[=DIR] where DIR is the location of the directory containing the lib and include directories" mean. Where do I do this and how? I'm assuming I'm supposed to edit a file and add that line or something, but what file do I edit and where in the file does this command go? Sorry for sounding like an idiot. Thanks for any help.
egmax
Forum Newbie
Posts: 5
Joined: Mon Nov 07, 2005 10:19 pm
Location: &#20013;&#22269;CHINA

Post by egmax »

file('your-files-url');


:lol:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

is fopen() doesn't work, file() won't either.. just FYI..
Post Reply