keeps21 Posted February 23, 2009 Share Posted February 23, 2009 I have the following function. It is designed to read a webpage and then show the contents. It works fine on my localhost, but not on the live server, only outputting "Subject: ", $subject isn't shown at all. Any ideas why? <?php function get_music_player() { error_reporting(E_ALL); $url = "www.example.com"; $subject = join('', file($url)); return "Subject: " . $subject; } ?> The file using the function is as follows. echo get_music_player(); Quote Link to comment https://forums.phpfreaks.com/topic/146532-solved-reading-another-webpage/ Share on other sites More sharing options...
sloth456 Posted February 23, 2009 Share Posted February 23, 2009 doesn't the url have to be prefixed with http:// Quote Link to comment https://forums.phpfreaks.com/topic/146532-solved-reading-another-webpage/#findComment-769282 Share on other sites More sharing options...
keeps21 Posted February 23, 2009 Author Share Posted February 23, 2009 Oops missed that out when typing it in here. It's in my actual file though Quote Link to comment https://forums.phpfreaks.com/topic/146532-solved-reading-another-webpage/#findComment-769284 Share on other sites More sharing options...
keeps21 Posted February 23, 2009 Author Share Posted February 23, 2009 Amended the function slightly, still working on localhost, but not on the live server. <?php function get_music_player() { $subject = file_get_contents('http://www.example.com/'); return $subject; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/146532-solved-reading-another-webpage/#findComment-769292 Share on other sites More sharing options...
sloth456 Posted February 23, 2009 Share Posted February 23, 2009 hmm, strange, looks fine to me. Do you think maybe your host prevents outgoing requests like that? Maybe you could try using cURL instead? Quote Link to comment https://forums.phpfreaks.com/topic/146532-solved-reading-another-webpage/#findComment-769304 Share on other sites More sharing options...
premiso Posted February 23, 2009 Share Posted February 23, 2009 Are you on shared hosting? If so then chances are they do now allow fopen_url, they may allow CURL but for file_get_contents to work the server has to have that option set (you can do a phpinfo to find this out). My bet is that is what is going on. Quote Link to comment https://forums.phpfreaks.com/topic/146532-solved-reading-another-webpage/#findComment-769306 Share on other sites More sharing options...
keeps21 Posted February 23, 2009 Author Share Posted February 23, 2009 Just checked my phpinfo. Local server allow_url_fopen is on Live server allow_url_fopen is off So presumably and ini_set('allow_url_fopen') will sort out the problem. Quote Link to comment https://forums.phpfreaks.com/topic/146532-solved-reading-another-webpage/#findComment-769308 Share on other sites More sharing options...
keeps21 Posted February 23, 2009 Author Share Posted February 23, 2009 Are you on shared hosting? If so then chances are they do now allow fopen_url, they may allow CURL but for file_get_contents to work the server has to have that option set (you can do a phpinfo to find this out). My bet is that is what is going on. Ah you beat me to it I'll have a go at changing the ini. Quote Link to comment https://forums.phpfreaks.com/topic/146532-solved-reading-another-webpage/#findComment-769310 Share on other sites More sharing options...
keeps21 Posted February 23, 2009 Author Share Posted February 23, 2009 As I'm on shared hosting I cannot use <?php ini_set('allow_url_fopen', 1); ?> Could any one enlighten me on how I would use curl instead of file_get_contents() ? Quote Link to comment https://forums.phpfreaks.com/topic/146532-solved-reading-another-webpage/#findComment-769317 Share on other sites More sharing options...
premiso Posted February 23, 2009 Share Posted February 23, 2009 <?php // create a new cURL resource $ch = curl_init(); // set URL and other appropriate options curl_setopt($ch, CURLOPT_URL, "http://www.example.com/"); curl_setopt($ch, CURLOPT_HEADER, 0); // grab URL and pass it to the browser curl_exec($ch); // close cURL resource, and free up system resources curl_close($ch); ?> curl_exec Quote Link to comment https://forums.phpfreaks.com/topic/146532-solved-reading-another-webpage/#findComment-769320 Share on other sites More sharing options...
keeps21 Posted February 23, 2009 Author Share Posted February 23, 2009 Sorry I'm a bit lost, Using the curl code posted above, how would I then echo out the contents of the page? Quote Link to comment https://forums.phpfreaks.com/topic/146532-solved-reading-another-webpage/#findComment-769325 Share on other sites More sharing options...
premiso Posted February 23, 2009 Share Posted February 23, 2009 Nope. Your host must not like that/had issues with it in the past. imo, switch hosts. There are plenty that at least allow Curl that are decently priced. You can try using Sockets but I doubt they will work given that the latter two did not. Quote Link to comment https://forums.phpfreaks.com/topic/146532-solved-reading-another-webpage/#findComment-769328 Share on other sites More sharing options...
keeps21 Posted February 23, 2009 Author Share Posted February 23, 2009 Figured it out. Edited the post before you replied - did a phpinfo on my local server - curl not installed. did another on my live server and it is installed so i've got it working now Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/146532-solved-reading-another-webpage/#findComment-769330 Share on other sites More sharing options...
keeps21 Posted February 23, 2009 Author Share Posted February 23, 2009 Yet another question for you. I'm noticing that curl_exec($ch) is outputting the contents of the webpage straight away. How do I assign the contents to a variable so that I can then process it? $contents = curl_exec($ch); The above is just outputting the contents. Quote Link to comment https://forums.phpfreaks.com/topic/146532-solved-reading-another-webpage/#findComment-769335 Share on other sites More sharing options...
premiso Posted February 23, 2009 Share Posted February 23, 2009 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_exec and curl_setopt Return Values Returns TRUE on success or FALSE on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure. Quote Link to comment https://forums.phpfreaks.com/topic/146532-solved-reading-another-webpage/#findComment-769338 Share on other sites More sharing options...
keeps21 Posted February 23, 2009 Author Share Posted February 23, 2009 Cheers. Thank you all very much for your help Quote Link to comment https://forums.phpfreaks.com/topic/146532-solved-reading-another-webpage/#findComment-769340 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.