Unable to use php functions on hostgator server - php

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);

Related

Error with the function file_get_contents() : dns involved or not? [duplicate]

This question already has answers here:
file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known
(6 answers)
Closed 3 years ago.
I am trying to run a small website that is hosted on a distant virtual server.
My code was working fine when I was running my tests locally, on my machine through Apache 2.0.
Now that it's hosted remotely ( Apache 2 + Webmin + latest version of PHP ), seems like I'm having some issue with the famous function file_get_contents. Looks like my server doesn't allow the resolution of any external domain.
I have read multiple topics and tried many solutions but seems like none of them has worked so far.
Here is the function I have a problem with
$url='https://bitpay.com/api/rates';
$json=json_decode( file_get_contents( $url ) );
$dollar=$btc=0;
foreach( $json as $obj ){
if( $obj->code=='USD' )$btc=$obj->rate;
}
echo "1 bitcoin=\$" . $btc . "USD<br />";
$dollar=1 / $btc;
echo "10 dollars = " . round( $dollar * 10,8 )."BTC";
exit();
returns me with this error whatever I try
Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known in
I tried to locate the file etc/host on my server but it's inexistent.
I have no idea how to setup/parameter any hostname.
I don't have a lot of options in my Webmin panel so I'm wondering what I can do to work toward the resolution of this problem.
I have added the line allow_url_fopen = on in my code but it doesn't work either.
Finally, I tried the curl alternative that so many of you are advising.
But when I try to echo or print_r the output/result, I get a blank page. Not even a single error message.
file_get_contents don't allow fetching external url until allow_url_fopen is set to On.
Since you have already set it to On. you can try this code:
<?php
function get_content($URL){
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $URL);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo get_content('https://bitpay.com/');
?>
However the page is not going to load properly because on the target site the absolute path for the resources(images, scripts, etc) are used. So need to the path to full URL.
I made a few changes to my php.ini file.
here is the content of my current php.ini file
allow_url_fopen = On;
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
But I realised something intriguing : regarding this line
ini_set('display_errors', 1);
If I don't add it in my php file where my code is, then the error are not displayed. Seems like my ini file is not a the right place or that taken into consideration by the server.
I'm really getting confused because it's very different from the local hosting
I feel my server configuration is null.
None of the config file can be found in the etc directory and I my php.ini file is " not working " obivously.
Anyone could solve this problem out without having access to the hosts.conf file ?

issue with fopen on google app engine

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');

Warning on loading XML data

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

load xml file in php - missing access rights?

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.

Copy file from https url

I crawled a web to get pdf documents in the web. for example, I got 9 PDFs documents from it, and the copy process for 8 PDFs runs well, except for one document. I don't know what's wrong, I've no problem with the connection. when I check out the url directly, it's ok too, I can get that document manually. The url starts with https, it that https problem? how to solve it? I use copy function :
copy($pdfLink, $savePath . basename($pdfLink));
the error result is :
Warning: copy(https://www.aclweb.org/anthology/J/J08/J08-1004.pdf) [function.copy]: failed to open stream: Invalid argument in D:\AppServ\www\suksesfunctionWrapper.php on line 19
Sounds to me like you need tweak a setting in php.ini file so you can make a https request. If you are hard coding the URL just replace https to http.
You might need to enable ssl in php as the file is from a https source.
I believe the extension enable is openssl. Look for the line that loads the php_openssl.dll in the php.ini and uncomment that.
Try this:
$opts = array(
"ssl"=>array(
"allow_self_signed"=>true,
"verify_peer"=>false,
"verify_peer_name"=>false
)
);
copy($pdfLink, $savePath . basename($pdfLink), stream_context_create($opts));

Categories