lard Posted August 29, 2014 Share Posted August 29, 2014 Hi all So I'm back to being a php newbie after not touching the stuff for years....and straight back to being pretty sure I'm just incredibly stupid. I want to grab the contents of a url onto a string so that I can search the string and use the contents. I think this should work: <?php $homepage = file_get_contents('http://www.example.com/'); echo "Homepage = " . $homepage; ?> But it doesn't. What am I doing wrong, or could there be an issue with my server that means it's not able to perform this maybe? (It's working fine for everything so far) Or is there another way of achieving this? Thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/290734-url-contents-onto-a-string/ Share on other sites More sharing options...
ginerjm Posted August 29, 2014 Share Posted August 29, 2014 I"ll throw my $.02 in and say perhaps the url you specified is not being opened since it is not technically a 'file'. You could check your get results and see? Quote Link to comment https://forums.phpfreaks.com/topic/290734-url-contents-onto-a-string/#findComment-1489301 Share on other sites More sharing options...
Solution ginerjm Posted August 29, 2014 Solution Share Posted August 29, 2014 Reading the manual like all good php programmers should do I see that there is an install option that must be enabled. Read up on it and see if that is your problem PS - your code worked just fine for me. Quote Link to comment https://forums.phpfreaks.com/topic/290734-url-contents-onto-a-string/#findComment-1489304 Share on other sites More sharing options...
lard Posted August 29, 2014 Author Share Posted August 29, 2014 Trouble is I'm not a good php programmer Many thanks, I figured that it may well be my settings, but thought it was worth asking before wasting a couple of hours. Quote Link to comment https://forums.phpfreaks.com/topic/290734-url-contents-onto-a-string/#findComment-1489306 Share on other sites More sharing options...
Jacques1 Posted August 29, 2014 Share Posted August 29, 2014 First of all, “doesn't work” tells us absolutely nothing. It could mean anything from “When I execute the script, my PC catches fire” to “There's a tiny issue with the HTML markup I get back”. If you want a good answer, you need to ask a good question. So what is the problem? What happens? Are you getting any errors? If not, are you sure your error reporting is on and up? In general, you can't just read a remote file like you read a local file. This is a special feature which is often turned off for security reasons. If it actually is turned off on your server, then PHP will tell you. But only if your error reporting is turned on and up. Quote Link to comment https://forums.phpfreaks.com/topic/290734-url-contents-onto-a-string/#findComment-1489309 Share on other sites More sharing options...
ginerjm Posted August 29, 2014 Share Posted August 29, 2014 Error reporting: error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', '1'); Place at top of your php Quote Link to comment https://forums.phpfreaks.com/topic/290734-url-contents-onto-a-string/#findComment-1489310 Share on other sites More sharing options...
lard Posted August 29, 2014 Author Share Posted August 29, 2014 First of all, “doesn't work” tells us absolutely nothing. It could mean anything from “When I execute the script, my PC catches fire” to “There's a tiny issue with the HTML markup I get back”. If you want a good answer, you need to ask a good question. So what is the problem? What happens? Are you getting any errors? If not, are you sure your error reporting is on and up? In general, you can't just read a remote file like you read a local file. This is a special feature which is often turned off for security reasons. If it actually is turned off on your server, then PHP will tell you. But only if your error reporting is turned on and up. Wow, someone got out of bed on the wrong side. Your opening gambit of 'First of all' may as well have been shouted at your bored wife because of your frustration at your underachieving kids. How about you try reading what's been written before wading in with your size 11s? If you read my code with your expert eye, and the fact that I've stated that my server is providing php perfectly adequately in other respects, you'll realise that there's not exactly a lot of errors possible. I mean, what am I going to get, a connection error? I've clearly made the code as simple as it could be to eliminate as many extraneous issues as possible. Okay, so I'm a newbie, I'm sure there's things that could return, that Mr Doesn't Realise He's On The Spectrum could point out in his anally retentive manner, but check out my response to the delightfully helpful 'ginerjm' after he / she very courteously reported that the code works, and therefore should work for me given the correct settings; I've concluded my problem's source and will address it. What is the point of your response Jacques? Oh yeah, back to your underlying frustration with wife, kids, life....whatever. I'd say thanks for your help but no thanks, you haven't been any. Do you find that talking to people like that face to face elicits positive responses? Or do you lack the emotional intelligence to be able to tell? Courtesy costs nothing. Not responding would have been easier. Responding in the way that you did, especially when it's unnecessary, gets zero respect from any self respecting person. What, you need to provide enough responses to get out of the forum whatever it is you get out of it? Many thanks to you ginerjm, you gave me enough information to direct me correctly. Apologies for responding in this way, but I'm fed up of people who clearly have excellent knowledge in a certain field being disrespectful and failing to understand where they fall down. I'll ask a further question if I'm unable to find what I need to, but should that be the case, I suspect I'll start a new thread. Cheerio. Quote Link to comment https://forums.phpfreaks.com/topic/290734-url-contents-onto-a-string/#findComment-1489339 Share on other sites More sharing options...
Jacques1 Posted August 30, 2014 Share Posted August 30, 2014 (edited) So you're a PHP newbie, yet you know exactly that my way of debugging code is completely wrong and doesn't help? Fascinating. I've been using PHP for quite a while now, and I can assure you that the internal PHP functions don't just silently break. When there's a problem, there's almost certainly an error message. For example, running your code with incorret settings yields those two error messages: This tells you exactly what is wrong and what you need to do to fix the problem. Even if you don't know how to interpret the messages, this is extremely valuable information for the people trying to help you. Now, as long as we're dealing with simple problems, we may sometimes be able to guess the issue even if we have absolutely no information. That's what ginerjm did: He tried the code, did some research and eventually came up with an intelligent guess. That's OK, and it appearently worked out this time. But guessing is always the last resort. It's what you do when the person on the other end is simply too stupid (or technically incompetent) to give you the relevant information. You are neither stupid nor technically incompetent. You've written this piece of code, so you are definitely smart enough to describe the problem. Please do. It will save everybody a lot of time, and you'll benefit from a better answer. Do you now see what I mean? - Edited August 30, 2014 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/290734-url-contents-onto-a-string/#findComment-1489342 Share on other sites More sharing options...
lard Posted August 30, 2014 Author Share Posted August 30, 2014 I'm not going to read that because you've clearly missed my point. Be nice, people will be nice back. Quote Link to comment https://forums.phpfreaks.com/topic/290734-url-contents-onto-a-string/#findComment-1489343 Share on other sites More sharing options...
Jacques1 Posted August 30, 2014 Share Posted August 30, 2014 If you want wishy-washy talk and a fake smile, programming is not for you. Go to Facebook or something. Quote Link to comment https://forums.phpfreaks.com/topic/290734-url-contents-onto-a-string/#findComment-1489346 Share on other sites More sharing options...
lard Posted August 31, 2014 Author Share Posted August 31, 2014 Not being a dick does not mean wishy washy fake smile rubbish. Quote Link to comment https://forums.phpfreaks.com/topic/290734-url-contents-onto-a-string/#findComment-1489452 Share on other sites More sharing options...
ginerjm Posted August 31, 2014 Share Posted August 31, 2014 Lard - a very nice response from you. Obviously a well-rounded and well-educated individual. Loved your post. Jacques1 - you could learn a lot from Lard in how to be nice to people. Sure you are a first class nerd and can converse well in PHP and lots of other highly technical fields, but you sorely lack common decency. Even your retorts lack respect. So what if you are correct? You have given me my share of abuse and toyed with me for hours. I take it because you eventually will provide some real help. Too bad we have to put up with your nonsense to get it. And now I suggest a moderator close this topic. We are definitely off course here, but I felt a need to let our newbie OP know that he is not alone here after facing the wrath of jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/290734-url-contents-onto-a-string/#findComment-1489453 Share on other sites More sharing options...
lard Posted September 1, 2014 Author Share Posted September 1, 2014 Thanks again ginerjm. I've located my ini file and the correct setting and now all is working as it should be. Thank you! And for the last bit Quote Link to comment https://forums.phpfreaks.com/topic/290734-url-contents-onto-a-string/#findComment-1489526 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.