Jump to content

URL contents onto a string


Go to solution Solved by ginerjm,

Recommended Posts

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!!

 

Link to comment
https://forums.phpfreaks.com/topic/290734-url-contents-onto-a-string/
Share on other sites

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.

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.

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: 

 

post-167590-0-12935900-1409358809.png

 

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? 

 

 

 

 

 

 

-

post-167590-0-12935900-1409358809_thumb.png

Edited by Jacques1

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.