file_get_contents() not able to fetch data? - php

I searched several posts similar to this but could not find an answer. I am probably missing out something small.
I am trying to read JSON contents from a URL as shown by my code:
$uri = "http://worldweatheronline.com/feed/weather.ashx?q=schruns,austria&format=json&num_of_days=5&key=8f2d1ea151085304102710";
echo "URI: [$uri] <br/>";
$file = file_get_contents($uri);
$error = error_get_last();
echo $error['message'];
If I open the URL in a browser, I can see the JSON contents of it. But the above mentioned code does not work. $file has a value of false. The error message is:
file_get_contents(http://worldweatheronline.com/feed/weather.ashx?q=schruns,austria&format=json&num_of_days=5&key=8f2d1ea151085304102710) [function.file-get-contents]: failed to open stream: No such file or directory.
Any suggestions? NOTE: my allow_url_fopen is set to 1 if it matters.
Edit:
Here is the entire PHP file.
http://pastebin.com/zCKEP9kG
Also, there is an fopen() in the code which opens an http file just fine.

I don't see anything wrong with the code, mate - it runs fine locally on my setup and I can get the file, and can do whatever I like with it.
Try it running on a different host - may be your php.ini is messed up and is messing up the script as well.. Or may be you've got problem with some other spot of the source.. may be if you shared the entire file source, or at least a bigger chunk of it..
Cheers!

Check this code, i think you can proceed further with this now --
<?php
$json_url = "http://worldweatheronline.com/feed/weather.ashx?q=schruns,austria&format=json&num_of_days=5&key=8f2d1ea151085304102710";
$json = file_get_contents($json_url);
$data = json_decode($json, TRUE);
echo "<pre>";
print_r($data);
echo "</pre>";
?>
#ascii-lime
its for #ascii-lime--
Check this (Edit#2)

Try This :
<?php
header('Content-Type: application/json');
$json = file_get_contents('http://worldweatheronline.com/feed/weather.ashx?q=schruns,austria&format=json&num_of_days=5&key=8f2d1ea151085304102710');
echo $json;
?>

Related

file_get_contents on local wamp server not working

I simply cant wrap my head around why this simple code won't work on my local wamp server where i have other sites running.
<?php
$file = file_get_contents('C:\Users\Computer\input.txt', true);
echo $file;
?>
The file is where it should be.
I get the following error:
Warning: file_get_contents(C:\Users\Computer\input.txt): failed to open stream: No such file or directory in C:\Users\Computer\Dropbox\htdocs\ny\www\php\day2\code.php on line 3
But the file is exactly where it should be.
The file has all possible permissions to true (read, write ect.) in windows except for "special permissions".
What is not working correctly?
Regards,
Patrick
The solution was the extension. It was not correctly parsed. This was checked by using #VolkerK suggesting to check
Does the output of var_export(glob('C:\Users\Computer*')); contain
input.txt? Or is it false/null?
Thanks to #VolkerK for the help solving the problem.
Since I incorrectly said that it might be because of backslash escaping, I guess I need to redeem by saying something useful :)
So try:
$file = 'C:\Users\Computer\input.txt';
if(is_file($file))
{
$string = file_get_contents($file);
}
else
{
die("Uh oh! Seems like $file has got problems!");
}
Also, you are passing the second argument as true for searching the include path. Since your filepath is absolute, I suspect if you need this argument to be passed at all.

Download file to server using API (it triggers prompt)

I want to store some data retrieved using an API on my server. Specifically, these are .mp3 files of (free) learning tracks. I'm running into a problem though. The mp3 link returned from the request isn't to a straight .mp3 file, but rather makes an ADDITIONAL API call which normally would prompt you to download the mp3 file.
file_put_contents doesn't seem to like that. The mp3 file is empty.
Here's the code:
$id = $_POST['cid'];
$title = $_POST['title'];
if (!file_exists("tags/".$id."_".$title))
{
mkdir("tags/".$id."_".$title);
}
else
echo "Dir already exists";
file_put_contents("tags/{$id}_{$title}/all.mp3", fopen($_POST['all'], 'r'));
And here is an example of the second API I mentioned earlier:
http://www.barbershoptags.com/dbaction.php?action=DownloadFile&dbase=tags&id=31&fldname=AllParts
Is there some way to bypass this intermediate step? If there's no way to access the direct URL of the mp3, is there a way to redirect the file download prompt to my server?
Thank you in advance for your help!
EDIT
Here is the current snippet. I should be echoing something, correct?
$handle = fopen("http://www.barbershoptags.com/dbaction.php?action=DownloadFile&dbase=tags&id=31&fldname=AllParts", 'rb');
$contents = stream_get_contents($handle);
echo $contents;
Because this echos nothing.
SOLUTION
Ok, I guess file_get_contents is supposed to handle redirects just fine, but this wasn't happening. So I found this function: https://stackoverflow.com/a/4102293/2723783 to return the final redirect of the API. I plugged that URL into file_get_contents and volia!
You seem to be just opening the file handler and not getting the contents using fread() or another similar function:
http://www.php.net/manual/en/function.fread.php
$handle = fopen($_POST['all'], 'rb')
file_put_contents("tags/{$id}_{$title}/all.mp3", stream_get_contents($handle));

php/html file is not displayed by server

I am extremely knew to working with databases and servers and just downloaded MAMP. I have a website I made in html and css and want to inject a php script to it. I have saved the file in .php and when I navigate to it from the localhost port on my browser, none of the html is displayed. It only shows a blank white page. There is probably a really obvious answer but I've been searching google for an hour and haven't found a solution.
This is the php script. It is wrapped in a tag everything else in the document is html.
<?PHP
$filelocation = "Text.txt";
if (!file_exists($filelocation)) {
echo "Couldn't find datafile, please contact the administrator.";
}
else {
$newfile = fopen($filelocation,"r");
$content = fread($newfile, filesize($filelocation));
fclose($newfile);
}
$content = stripslashes($content):
$content = htmlentities($content):
$content = nl2br($content);
echo $content;
?>
Most likely there's an error in your PHP code and it can't be parsed. Check the server logs to see what the error message is.
That seems to be valid PHP at a glance.
You could read the file more easily by doing...
$content = file_get_contents($filelocation);
but that's incidental.
Turn on Error reporting in your php.ini file and then restart your webserver. This should give more detailed error information. You should also check your server error logs as there's usually something in there too.
Are you getting an HTTP 500 response code with the blank page? Also, are you sure the file in question actually has any contents?
You have two syntax errors in your code:
$content = stripslashes($content):
$content = htmlentities($content):
They must end with semicolon
$content = stripslashes($content);
$content = htmlentities($content);
Besides that, your errors are likely catched and written to logs as said by others.
Most probably you have a php / php syntax error.
On the first line of your .php file write the following:
<?php error_reporting(E_ALL); ?>
this should make the interpreter to show you the errors you have.
Also, more details about your problem won't hurt.

PHP - file_get_contents() automatically echoes to the HTML page

I have a page on a remote server with the following line:
$contents = file_get_contents($search_url);
which automatically echoes $contents to the HTML page no matter what I do. It's as if I have done the following:
$contents = file_get_contents($search_url);
echo $contents;
What could be causing PHP to do this? Is there any configuration item that needs changing?
It might be worth taking a look at this bit of your code. If you comment it out, does the same thing still happen?
<?php if (isset($debug) && isset($ret_value)):?>
<pre>
<?php print_r($ret_value) ?>
</pre>
<?php endif; ?>
This does appear to echo what you have previously fetched in the file_get_contents().
It was a STUPID bug - the variable $contents is being used in the included file as well as in the function it is included from. This caused the issue - nothing wrong with file_get_contents!

Include whole content of a file and echo it

I need to echo entire content of included file. I have tried the below:
echo "<?php include ('http://www.example.com/script.php'); ?>";
echo "include (\"http://www.example.com/script.php\");";
But neither works? Does PHP support this?
Just do:
include("http://www.mysite.com/script.php");
Or:
echo file_get_contents("http://www.mysite.com/script.php");
Notes:
This may slow down your page due to network latency or if the other server is slow.
This requires allow_url_fopen to be on for your PHP installation. Some hosts turn it off.
This will not give you the PHP code, it'll give you the HTML/text output.
Shortest way is:
readfile('http://www.mysite.com/script.php');
That will directly output the file.
Echo prints something to the output buffer - it's not parsed by PHP. If you want to include something, just do it
include ('http://www.mysite.com/script.php');
You don't need to print out PHP source code, when you're writing PHP source code.
Not really sure what you're asking, but you can't really include something via http and expect to see code, since the server will parse the file.
If "script.php" is a local file, you could try something like:
$file = file_get_contents('script.php');
echo $file;
This may not be the exact answer to your question, but why don't you just close the echo statement, insert your include statement, and then add a new echo statement?
<?php
echo 'The brown cow';
include './script.php';
echo 'jumped over the fence.';
?>
Matt is correct with readfile() but it also may be helpful for someone to look into the PHP file handling functions
manual entry for fpassthru
<?php
$f = fopen($filepath, 'r');
fpassthru($f);
fclose($f);
?>

Categories