I have a script that runs on a site completely separate from mine. When I run it, the result of that script is a number - that's it. No HTML around it or anything. I want to be able to drop that result into a single variable as easily as possible. Something like: $the_result = file("http://www.whatever.com/specialscript.php"); Of course, doing that puts the result in an array, which I'd need to go through. I'm going to have to call this script on a variety of servers quite often, so I need the most efficient way to get the result back. Make sense?
$myvar = file_get_contents($url); PHP: Does exactly what you want, gets the result of a URL and returns it as a string.
One more quick question about this. If the file isn't there (404), it comes back ugly (big error message on the page). How can I trap that error and display my own error message?
The problem is that as soon as I run file_get_contents the error appears. I could see that and show a custom error after the fact, but the mess is already on their screen. How can I prevent that?
Ok, now another problem. For one of them, I'm getting the following error: Warning: file_get_contents() [function.file-get-contents]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/xxxxxx/public_html/cron/userposts.php on line 23 Warning: file_get_contents(http://www.xxx.com/file.php?userid=57) [function.file-get-contents]: failed to open stream: Success in /home/xxxxxx/public_html/cron/userposts.php on line 23 Code (markup): Any ideas? The addresses are correct and I can pull them up in a browser.