Jump to content

Issues with file() and file_get_contents()


sharpjx

Recommended Posts

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

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/159804-issues-with-file-and-file_get_contents/
Share on other sites

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.

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.