HaLo2FrEeEk Posted December 29, 2010 Share Posted December 29, 2010 I'm writing a quick little script to grab some information from Stickam.com, specifically using the "API" they have set up to get a user id from a provided username, and a username from a provided userid. These are both publically accessible pieces of information, so I'm not getting into anything I shouldn't, I'm just using the publically accessible API instead of scraping the HTML. For some reason I can't explain though, I can't seem to get the contents of the page using PHP. Here's my code: $username = @$_GET['username']; if(!$username) { die("no username"); } $url = "http://www.stickam.com/servlet/ajax/getLiveUser?uname=".$username; $html = file_get_contents($url, false, $context); echo "<pre>"; echo $html; echo "</pre>"; But everytime I load this page on my server, I get this response: Warning: file_get_contents(http://www.stickam.com/servlet/ajax/getLiveUser?uname=[username]) [function.file-get-contents]: failed to open stream: Connection timed out in [file] on line 7 I can load it just fine on my Apache server running on my personal computer, and in my browser (both IE9 and Firefox), but not on my website. I've tried mimicking the headers that my browser sends and creating a stream context to send with the file_get_contents() call, but so far nothing seems to work. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/222889-why-does-this-connection-time-out/ Share on other sites More sharing options...
dragon_sa Posted December 29, 2010 Share Posted December 29, 2010 does it make any difference if you do this $url = "http://www.stickam.com/servlet/ajax/getLiveUser?uname=$username"; $html = file_get_contents('$url', false, $context); Quote Link to comment https://forums.phpfreaks.com/topic/222889-why-does-this-connection-time-out/#findComment-1152504 Share on other sites More sharing options...
trq Posted December 29, 2010 Share Posted December 29, 2010 does it make any difference if you do this $url = "http://www.stickam.com/servlet/ajax/getLiveUser?uname=$username"; $html = file_get_contents('$url', false, $context); Why on earth would that work? Variables are not interpolated within single quotes, nor do they need to be within quotes at all. Op: Have you tried the url in a browser? It's not working for me. Quote Link to comment https://forums.phpfreaks.com/topic/222889-why-does-this-connection-time-out/#findComment-1152514 Share on other sites More sharing options...
dragon_sa Posted December 29, 2010 Share Posted December 29, 2010 I thought it may be an issue as this is a quote I got from the php.net/manual // Open the file using the HTTP headers set above $file = file_get_contents('http://www.example.com/', false, $context); as you can see this is in single quotes Quote Link to comment https://forums.phpfreaks.com/topic/222889-why-does-this-connection-time-out/#findComment-1152526 Share on other sites More sharing options...
trq Posted December 29, 2010 Share Posted December 29, 2010 That is a string is why. Quote Link to comment https://forums.phpfreaks.com/topic/222889-why-does-this-connection-time-out/#findComment-1152528 Share on other sites More sharing options...
HaLo2FrEeEk Posted December 29, 2010 Author Share Posted December 29, 2010 The URL does work in a browser, you just have to provide a username for an online user. If the user is not currently online, the return will simply be: [ ] if the usr doesn't exist, or no name is provided, then you'll get an error page with an "HTTP Status 409" message. Try going here: http://www.stickam.com/servlet/ajax/sessionMembers?sessionType=live And getting a username from the list, it'll be the first value in each of the arrays. Put that username after the URL: http://www.stickam.com/servlet/ajax/getLiveUser?uname= And it should work. Like I said, I can do it just fine using the same script on my local computer's Apache server. I literally wrote the script on my local server, got it working, then saved the same script to my FTP and tried to load it. Is there some way to create a stream context using an exact copy of the header data that the browser requesting the page used? Come to think of it, is there actually any way to view the header data that my browser sent? Quote Link to comment https://forums.phpfreaks.com/topic/222889-why-does-this-connection-time-out/#findComment-1152563 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.