sharpjx Posted May 27, 2009 Share Posted May 27, 2009 I am scratching my head at this one. I am trying to do the following: $data = file('http://sunsolve.sun.com/search/printfriendly.do?assetkey=118367-04'); print_r ($data); That does not work. $data is empty although I know fully well that there is a page of data on that Sun Microsystems patch. I tried using file_get_contents() to read it as a string. Same thing. The variable is empty. The only thing that 'kinda' works is $data = system("curl http://sunsolve.sun.com/search/printfriendly.do?assetkey=118367-04"); The problem is that I want to capture the output into $data and process it as it contains some info I need, but all that system does is display the output of 'curl' without passing it on to the variable. I could output to a file and use system() to read that file, but that is not very elegant. There has to be a way to do this using file() or file_get_contents(). BTW, I know that file() and file_get_contents() work fine for other web pages on my system so this is not a php.ini issue. I am fairly sure that the issue is with the Sunsolve web server at the other end and how it interacts with PHP. How can I verify this and how can I overcome this problem to capture the output of http://sunsolve.sun.com/search/printfriendly.do?assetkey=118367-04, pass it to a variable and start searching within the output? Help! François Quote Link to comment https://forums.phpfreaks.com/topic/159804-issues-with-file-and-file_get_contents/ Share on other sites More sharing options...
corbin Posted May 27, 2009 Share Posted May 27, 2009 Sun probably doesn't like PHP pages requesting their content. You'll either need to use cURL or sockets. http://php.net/curl http://php.net/fsockopen If you go the socket route, you'll need to know some HTTP protocol stuff, but nothing that's super complex. Quote Link to comment https://forums.phpfreaks.com/topic/159804-issues-with-file-and-file_get_contents/#findComment-842884 Share on other sites More sharing options...
sKunKbad Posted May 27, 2009 Share Posted May 27, 2009 you can create a stream context within file_get_contents, and add a user-agent string. That might be your easy answer. See the file_get_contents page in the docs if you haven't done this before. Quote Link to comment https://forums.phpfreaks.com/topic/159804-issues-with-file-and-file_get_contents/#findComment-842904 Share on other sites More sharing options...
sharpjx Posted May 27, 2009 Author Share Posted May 27, 2009 Thanks for the pointers, this helped me tremendously. Here is the magic code to resolve this issue $ch = curl_init(); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/3.0.10'); curl_setopt($ch, CURLOPT_URL, "http://sunsolve.sun.com/search/printfriendly.do?assetkey=118367-04"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); echo $output; The magic is on line # 2 where CURLOPT_USERAGENT is set. sunsolve.sun.com seems to like this better than whatever PHP was sending before. Cheers and thanks! François Quote Link to comment https://forums.phpfreaks.com/topic/159804-issues-with-file-and-file_get_contents/#findComment-843261 Share on other sites More sharing options...
thebadbad Posted May 27, 2009 Share Posted May 27, 2009 cURL is not required, you can set the user agent string with ini_set(), and then use one of your former methods. ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/3.0.10'); Quote Link to comment https://forums.phpfreaks.com/topic/159804-issues-with-file-and-file_get_contents/#findComment-843275 Share on other sites More sharing options...
sharpjx Posted May 27, 2009 Author Share Posted May 27, 2009 Yes, I just realized that by playing around a bit more. This finally works as expected: ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/3.0.10'); $file = file_get_contents('http://sunsolve.sun.com/search/printfriendly.do?assetkey=118367-04', false); The page goes into $file and can be analyzed. Thank you all. Quote Link to comment https://forums.phpfreaks.com/topic/159804-issues-with-file-and-file_get_contents/#findComment-843317 Share on other sites More sharing options...
sKunKbad Posted May 28, 2009 Share Posted May 28, 2009 ...sunsolve.sun.com seems to like this better than whatever PHP was sending before. It's not just sun.com; many servers will reject requests if user-agent isn't set. Quote Link to comment https://forums.phpfreaks.com/topic/159804-issues-with-file-and-file_get_contents/#findComment-843770 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.