I am using following code to get current time (hours) in Los Angeles
$response_xml_data = file_get_contents('http://www.earthtools.org/timezone-1.1/34.05223/-118.24368');
$data = simplexml_load_string($response_xml_data);
$dt = new DateTime($data->localtime);
$time = $dt->format('H:i:s');
list($hour, $min, $sec) = explode(':', $time);
echo $hour;
This code is working fine offline, but i am getting following warning message when i put this file online:
Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in [PHP file name and line number]
Warning: file_get_contents(http://www.earthtools.org/timezone-1.1/34.05223/-118.24368)
[function.file-get-contents]: failed to open stream: no suitable wrapper could be
found in [PHP file name and line number]
It looks like i am using a wrong method to read XML data, please suggest?
This means that on the server you call "online" the HTTP wrapper is disabled for file functions in PHP.
I believe this is caused by having php's allow_url_fopen setting disabled on your server. This prevents the https wrapper from being available, or something like that.
You can enable allow_url_fopen in your php.ini file and this should error should go away. Not sure of other repercussions of this so know what you are changing before you change it.
Ref: http://php.net/manual/en/filesystem.configuration.php
Basically, you just need to override php default configuration open php.ini
allow_url_fopen = ON
Related
I'm having the issue with fopen() on google app engine. Works normal 3-4 times and than this response from server.
Message: fopen(https://website_url/image_name.png): failed to open
stream: Connection closed.
Any idea how to troubleshoot what happened and why?
$image = "https://website_url/image_name.png";
$f = fopen($image,'rb');
php ini has allow_url_fopen = On
First make sure you have the correct path.
You need to give fopen the full path of the file, and you don't need chdir() at all. Try this version:
$path_to_file='url';
$fh = fopen($path_to_file.$filename, 'w') or die('Permission error');
I need my website to be able to use fopen() to access files on my file streaming server, which our admin has configured to use port 8000. But it errors out and I believe it's because of the port number. I have allow_url_fopen enabled in php.ini too, so I don't think that's the issue.
Warning: fopen(http://129.237.213.244:8000/mp3/First_8864.mp3) [function.fopen]:
failed to open stream: Connection refused in _npp_get_mp3() (line 23 of
/home2/ab83417/public_html/drupal/sites/all/modules/npr_player_pack/npp_library.php).
So my question is: Can I somehow tell the webserver or our stream server (icecast, btw) to stop being so picky, or should I use some other php function like curl?
You should make sure that you have allow_url_fopen set to 1.
Launch the following script:
<?php
error_reporting(E_ALL);
ini_set('display_errors','1');
echo ini_get('allow_url_fopen')."<br />";
$x = file_get_contents('http://129.237.213.244:8000/mp3/First_8864.mp3');
$y = fopen('http://129.237.213.244:8000/mp3/First_8864.mp3',"rb");
It gives me simple 1 and no warnings. If you also get 1 but have warnings you should run the script in other browser and change your ip earlier (for example vpn). It's possible that this site has banned your ip so you cannot connect with this site/url.
I write a php script and run it on hostgator server and it is giving this error:
Warning: file_get_contents(
http://affiliates.kissmyads.com/stats/lead_report.json?api_key=AFFRd3xa8hRB3xOj7KsPnmQhlFYgNE&start_date=2014-06-24&end_date=2014-06-24)
[function.file-get-contents]: failed to open stream: No such file or
directory in www/barpy-money.in/app/kissmyads.php on line 10
It is unable to load function file-get-contents(). I am using this function directly in my program. When I run this code in joomla article it works fine but on server it gives error. Can anyone tell me why this is happening?
This is my code:
<?php
require_once 'include/DB_Connect.php';
// connecting to database
$db = new DB_Connect();
$db->connect();
$url = ' http://affiliates.kissmyads.com/stats/lead_report.json?api_key=my_api_key&start_date=2014-06-24&end_date=2014-06-24';
$result = file_get_contents( $url ); // line number 10
echo '<pre>';
print_r( json_decode( $result ) );
echo '</pre>'; ?>
php.net documentation for this function has a tip at the bottom:
Tip
A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename.
In order to do this you will need to edit your php.ini file. Per hostgators instructions:
allow_url_fopen, how to enable
This can be done via your php.ini file by adding the following line:
allow_url_fopen = On
The php.ini file is where you declare changes to your PHP settings. You can edit the existing php.ini, or create a new text file in any subdirectory and name it php.ini.
http://support.hostgator.com/articles/specialized-help/technical/allow_url_fopen-how-to-enable
Also you have a space in the $url variable before your http.
check you have enabled in your php.ini
allow_url_fopen = on
try to check phpinfo()
or may be url doesn't exist you given in file_get_contents() also do not add spaces in url you need to set like:-
$url = "http://affiliates.kissmyads.com/stats/lead_report.json?api_key=my_api_key&start_date=2014-06-24&end_date=2014-06-24";
$result = file_get_contents($url);
I want to read a XML file from a certain link with the following code
$filename = 'http://XXXXX/rss.xml';
$xml = simplexml_load_file($filename);
When I try, I get the following error messages:
wrapper is disabled in the server configuration by allow_url_fopen=0
failed to open stream: no suitable wrapper could be found in
I/O warning : failed to load external entity
Whats the problem? Can it be that the server doesn´t support simplexml_load_file?
When allow_url_fopen is set to 0, you have no permissions to acces URL objects like files. Read official manual for more information.
You can not change this value via .htaccess, so you have to set allow_url_fopen=1 in your php.ini file. If you're using a shared hosting or some kind of it, you should contact hosting technical support and describe them your problem.
This problem seems to have been discussed in the past everywhere on google and here, but I have yet to find a solution.
A very simple fopen gives me a
PHP Warning: fopen(http://www.google.ca): failed to open stream: HTTP request failed!".
The URL I am fetching have no importance because even when I fetch http://www.google.com it doesnt work. The exact same script works on different server. The one failing is Ubuntu 10.04 and PHP 5.3.2. This is not a problem in my script, it's something different in my server or it might be a bug in PHP.
I have tried using a user_agent in php.ini but no success. My allow_url_fopen is set to On.
If you have any ideas, feel free!
It sounds like your configuration isn't allowed to use file functions, which is common these days because of security concerns. If you have the cURL libraries available to you, I would recommend trying those out.
PHP: cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.google.ca/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$file = curl_exec($ch);
curl_close($ch);
echo $file;
Check that your php.ini config is set to allow fopen to open external URL's:
allow_url_fopen "1"
http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
I'm not at all sure about whether this is the problem or not, but I know in the past I've had problems with opening URLs with fopen, often due to php.ini's allow_url_fopen or other unknown security settings
You may want to try cURL in PHP, which often works for me, you'll find an example really easily by googling that.
Check your phpinfo output - is http present under Registered PHP Streams?
Are you getting "HTTP request failed" without further details? The socket timeout could be expired. This default to 60 seconds. See: http://php.net/manual/en/function.socket-set-timeout.php