0

Try this sample code I threw together to illustrate a point:

<?php 
$url = "http://www.amazon.com/gp/offer-listing/B003WSNV4E/";
$html = file_get_contents($url);
echo($html);
?>

The amazon homepage works fine using this method (it is echoed in the browser), but this page just doesn't output anything. Is there a reason for this, and how can I fix it?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Rampage
  • 3
  • 2
  • Works fine here... could it be something with your computer? Firewall? – user852091 Aug 22 '11 at 21:50
  • Darn, stack overflow is messing up my links. The correct link is here: http://www.amazon.com/gp/offer-listing/B003WSNV4E/ – Rampage Aug 22 '11 at 21:50
  • Use Firebug's Net tab or HTTPFox to view the browser-server exchange - it's not a simple url - amazon does a redirect. – Marc B Aug 22 '11 at 21:50
  • @Rampage Both the URL in the question and the updated one in your comment work for me. – AgentConundrum Aug 22 '11 at 21:56
  • Related Question: [How to search html file for simple string?](http://stackoverflow.com/questions/7153704/how-to-search-html-file-for-simple-string/7153836) – hakre Aug 22 '11 at 21:58
  • @hakre: How is that related? That question is about searching html for a string. Mine is simply to try to get an html file even downloaded. I actually saw that question and got an idea for something so I started working on it and encountered this problem. – Rampage Aug 22 '11 at 22:00
  • @AgentConundrum: Does it work through a php file? Or just by simply visiting the link? I can't get it to echo in my php. – Rampage Aug 22 '11 at 22:01
  • @Marc: I've never used those utilities before, so maybe I'm doing it wrong but it only seems to be downloading this page: http://www.amazon.com/gp/offer-listing/B003WSNV4E/ref=dp_olp_new?ie=UTF8&condition=new – Rampage Aug 22 '11 at 22:02
  • @Rampage: It uses that exact link and according to the OP it is solved with the code fragment you posted above. – hakre Aug 22 '11 at 22:03
  • @hakre: Please don't go blind on me. It's actually a different link (mine is a light bulb, his is some tablet thingie). Also, it looks like he got his working but mine definitely isn't. That's why I came here for help. – Rampage Aug 22 '11 at 22:05
  • @Rampage: Here is another related one, which might help you trouble shooting your problem: [Why doesn't file_get_contents work?](http://stackoverflow.com/questions/6724467/why-doesnt-file-get-contents-work/6724479#6724479) – hakre Aug 22 '11 at 22:10

1 Answers1

1

I think your problem is that you're misunderstanding your own code.

You made this comment on the question (emphasis mine):

I've never used those utilities before, so maybe I'm doing it wrong but it only seems to be downloading this page: https://www.amazon.com/gp/offer-listing/B003WSNV4E/ref=dp_olp_new?ie=UTF8&condition=new

This implies to me that an Amazon page is appearing in your browser when you run this code. This is entirely expected.

When you try to download https://rads.stackoverflow.com/amzn/click/B003WSNV4E, you're being redirected to https://www.amazon.com/gp/offer-listing/B003WSNV4E/ref=dp_olp_new?ie=UTF8&condition=new which is the intent of StackOverflow's RADS system.

What happens from there is your code is loading the raw HTML into your $html variable and dumping it straight to the browser. Because you're passing raw HTML to the browser, the browser is interpreting it as such, and it tries (and succeeds) in rendering the page.

If you just want to see the code, but not render it, then you need to convert it into html entities first:

echo htmlentities($html);
Cœur
  • 37,241
  • 25
  • 195
  • 267
AgentConundrum
  • 20,288
  • 6
  • 64
  • 99
  • 1
    or do a `header('Content-type: text/plain');` – Marc B Aug 22 '11 at 22:12
  • @Marc Well sure, if you want to get all fancy with it. ;) Seriously though, you're absolutely right. It would just also cause issues if OP wanted to output any other HTML on his page. – AgentConundrum Aug 22 '11 at 22:15
  • @AgentConundrum: Thanks for your help. However, I think you misunderstand me. The page I'm using is the page that came directly from the URL in my browser. Stack Overflow changed it (maybe because I'm a new user?) to be something else. I don't see *any* redirection in my browser using this link: `http://amazon.com/gp/offer-listing/B003WSNV4E` – Rampage Aug 22 '11 at 22:17
  • @Rampage I'm getting lost here. What does redirection have to do with this? Both the RADS link and the Amazon link you keep posting work fine for me. What are you expecting to happen exactly? – AgentConundrum Aug 22 '11 at 22:20
  • @Agent: Okay, do you have a live server somewhere you can test this script? Can you post a screenshot of that script running and displaying ***anything*** other than a blank page? – Rampage Aug 22 '11 at 22:21
  • @Agent: This is what mine looks like: https://img.skitch.com/20110823-fatribsqiaxfp4rewfpj6disue.png – Rampage Aug 22 '11 at 22:30
  • @Agent: I really appreciate the time you took to make those. However, did you see my screenshot? Why is that happening? – Rampage Aug 22 '11 at 22:32
  • @Rampage I see your issue now. Odd that I can't repro it here. What is your `error_reporting` set at? Maybe some error messages are being ignored? Try putting `error_reporting(E_ALL | E_STRICT);` at the top of the script and seeing if any errors pop up. – AgentConundrum Aug 22 '11 at 22:34
  • @Agent: Nope, doesn't output anything new or any errors. It's really strange; that's why I just can't figure this out. I've never had this problem before. – Rampage Aug 22 '11 at 22:36
  • @Rampage: That's the weirdest thing. I don't know what else I can offer to help you. Sorry. – AgentConundrum Aug 22 '11 at 22:37
  • Dear me. Oh well, thank you for your help, I do appreciate it! – Rampage Aug 22 '11 at 22:39
  • (Removed links since they were on my personal site, and I don't want the clutter.) – AgentConundrum Aug 22 '11 at 22:45
  • Using `cURL` seemed to solve the issue. Also, I noticed that using the `www` in front of the domain helped. Now it's working! Thanks for your help. – Rampage Aug 22 '11 at 22:58
  • I'm just rewarding you for your help. I hate leaving questions marked as unanswered even if they're answered. ;) – Rampage Aug 22 '11 at 23:07