Author Topic: file_get_contents  (Read 1763 times)

0 Members and 1 Guest are viewing this topic.

Offline Otis

  • (aka Paul)
  • Forecaster
  • *****
  • Posts: 728
    • Lake Huron Weather
file_get_contents
« on: June 03, 2017, 02:47:50 PM »
I have been using the following to get the recreational beach forecast for my site.
Code: [Select]
<div id="main-copy">
<h1><?php langtrans('Great Lakes Recreational Beach Forecast'); ?></h1>
<pre>
<?php
$a 
file_get_contents ("http://tgftp.nws.noaa.gov/data/raw/fz/fzus53.kapx.srf.apx.txt");
echo (
$a);
?>

</pre>   
</div><!-- end main-copy -->

Today, it just stopped - is shows a blank page where the text forecast should be.

I'm guessing it has to do with recent changes with Gov sites but;
I am at a loss as to why so any input is appreciated.

Note: I'm trying to learn/teach myself php; ongoing process.

Thanks for any assistance.

CW3699

Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9297
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: file_get_contents
« Reply #1 on: June 03, 2017, 03:28:12 PM »
It's likely that the tgftp.nws.noaa.gov site has started enforcing the requirement to have a User-agent: defined with the http request.

Try using
Code: [Select]
<div id="main-copy">
<h1><?php langtrans('Great Lakes Recreational Beach Forecast'); ?></h1>
<pre>
<?php
   $STRopts 
= array(
  'http'=>array(
  'method'=>"GET",
  'protocol_version' => 1.1,
  'header'=>"Cache-Control: no-cache, must-revalidate\r\n" .
"Cache-control: max-age=0\r\n" .
"Connection: close\r\n" .
"User-agent: Mozilla/5.0 (raw forecast)\r\n" .
"Accept: text/html,text/plain\r\n"
  ),
  'https'=>array(
  'method'=>"GET",
  'protocol_version' => 1.1,
  'header'=>"Cache-Control: no-cache, must-revalidate\r\n" .
"Cache-control: max-age=0\r\n" .
"Connection: close\r\n" .
"User-agent: Mozilla/5.0 (raw forecast)\r\n" .
"Accept: text/html,text/plain\r\n"
  )
);

   
$STRcontext stream_context_create($STRopts);

$a file_get_contents ("http://tgftp.nws.noaa.gov/data/raw/fz/fzus53.kapx.srf.apx.txt",false,$STRcontext);
echo (
$a);
?>

</pre>   
</div><!-- end main-copy -->
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline Otis

  • (aka Paul)
  • Forecaster
  • *****
  • Posts: 728
    • Lake Huron Weather
Re: file_get_contents
« Reply #2 on: June 03, 2017, 03:35:49 PM »
Well I tired your code and still the blank page.  ](*,)

CW3699

Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9297
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: file_get_contents
« Reply #3 on: June 03, 2017, 04:09:03 PM »
You don't have the check-fetch-times.php utility loaded on your site, so I can't check remotely about your settings.
You can download the utility from here and use check-fetch-times.php?show=info to see the current PHP/template setup and check-fetch-times.php?show=versions to see what core template files may need updates :)

I did try the code locally, and it works fine, so it may be that your PHP needs to have

allow_url_fopen = on

in a php.ini to allow file_get_contents() to read URLs.  Using the new code is still recommended since all NWS sites will eventually require User-agent: strings (and will likely convert to https only).
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline Otis

  • (aka Paul)
  • Forecaster
  • *****
  • Posts: 728
    • Lake Huron Weather
Re: file_get_contents
« Reply #4 on: June 03, 2017, 04:15:05 PM »
Thanks Ken,

So, I have loaded the check-fetch-times file - side note:  I have loaded this file multiple times and it seems to "disappear" from my site after a while, strange.

Can you help with the php.ini file? May be in over my head?


CW3699

Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9297
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: file_get_contents
« Reply #5 on: June 03, 2017, 04:20:03 PM »
The check-fetch-times.php?show=info shows
Quote
Webserver OS: Linux host16.webserveralpha.com 3.10.0-327.10.1.el7.x86_64 #1 SMP Tue Feb 16 17:03:50 UTC 2016 x86_64
PHP Version: 7.0.19
Document root: /home/myweather/public_html
allow_url_fopen = off
allow_url_include = off

so that is likely the root cause of the issue.  They may have upgraded your site from PHP 5.x to 7.0.19 recently.

look in your website document root (same directory as all the wx...php pages) for a php.ini file .. if it exists, download it.  If it doesn't exist, create a file named php.ini in your local directory for your website (same one where the wx...php pages exist).

Add (or just insert) a line:
Code: [Select]
allow_url_fopen = onto the file, and upload it to your site.. then try the problem page again.
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline Otis

  • (aka Paul)
  • Forecaster
  • *****
  • Posts: 728
    • Lake Huron Weather
Re: file_get_contents
« Reply #6 on: June 03, 2017, 04:33:04 PM »
Still no joy Ken  :sad:

The ini file was not there so I created it from within my cpanel and the command you referenced is now there but nothing.


CW3699

Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9297
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: file_get_contents
« Reply #7 on: June 03, 2017, 04:36:39 PM »
Well, the check-fetch-times.php?show=info still shows
Quote
Webserver OS: Linux host16.webserveralpha.com 3.10.0-327.10.1.el7.x86_64 #1 SMP Tue Feb 16 17:03:50 UTC 2016 x86_64
PHP Version: 7.0.19
Document root: /home/myweather/public_html
allow_url_fopen = off
allow_url_include = off

Try using the cPanel PHP options settings.. there should be an area where you can either tick or add the

allow_url_fopen = on

to their PHP engine's defaults.
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline Otis

  • (aka Paul)
  • Forecaster
  • *****
  • Posts: 728
    • Lake Huron Weather
Re: file_get_contents
« Reply #8 on: June 03, 2017, 04:41:42 PM »
Yes I did that and the ini file shows both of the options you mentioned as now on.
Strange.

CW3699

Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9297
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: file_get_contents
« Reply #9 on: June 03, 2017, 04:52:37 PM »
The check-fetch-times.php shows

allow_url_fopen = off
allow_url_include = off

and it should show

allow_url_fopen = on
allow_url_include = off

The allow_url_include should always be off (to prevent miscreants from remotely including code to run on your webserver).

I suggest calling GoDaddy tech support about the issue.. they can get it fixed quickly.  Tell them you want allow_url_fopen enabled on your site(s).
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline Otis

  • (aka Paul)
  • Forecaster
  • *****
  • Posts: 728
    • Lake Huron Weather
Re: file_get_contents
« Reply #10 on: June 03, 2017, 04:55:49 PM »
Thanks a lot for your help Ken, I'll call my ISP (I don't use GoDaddy) and see if they can assist.
Again, appreciate your help  :-)

CW3699

Offline Otis

  • (aka Paul)
  • Forecaster
  • *****
  • Posts: 728
    • Lake Huron Weather
Re: file_get_contents
« Reply #11 on: June 03, 2017, 05:00:20 PM »
Ken, waiting on my ISP so we can talk.

In the meantime I found a place in my cPanel where I can set the PHP version.  I rolled it back to 5.6 and all is working now (other pages that quit too).

I'll still talk to my ISP but the current version of PHP seems to be working.

CW3699

Offline Jáchym

  • Meteotemplate Developer
  • Forecaster
  • *****
  • Posts: 8605
    • Meteotemplate
Re: file_get_contents
« Reply #12 on: June 03, 2017, 06:41:14 PM »
Did you try replacing file_get_contents with cURL?

Inserting this function:

Code: [Select]
function curlMain($url,$timeout){
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_USERAGENT,"Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0");
$data = curl_exec($ch);
curl_close($ch);
return $data;
}

And then instead of

file_get_contents(...)

use

curlMain(...)


Offline Otis

  • (aka Paul)
  • Forecaster
  • *****
  • Posts: 728
    • Lake Huron Weather
Re: file_get_contents
« Reply #13 on: June 03, 2017, 06:56:13 PM »
Did you try replacing file_get_contents with cURL?

Inserting this function:

Code: [Select]
function curlMain($url,$timeout){
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_USERAGENT,"Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0");
$data = curl_exec($ch);
curl_close($ch);
return $data;
}

And then instead of

file_get_contents(...)

use

curlMain(...)

Jachym, no I did not try using that function.  Since I reverted back to an earlier PHP version and everything works I am staying with that.  I still need to contact my ISP and talk to them about it and why it happened with their PHP upgrade.

Thanks for the input though.

CW3699

Offline R_o_B

  • WxElement panel
  • Senior Member
  • *****
  • Posts: 85
Re: file_get_contents
« Reply #14 on: June 04, 2017, 08:26:29 PM »
Just so that you know, active support for version 5.6 of PHP ended on January 19, 2017 - only critical security issues will be fixed. The end of life for version 5.6 of PHP will be on December 31, 2018.

Quote
I'm trying to learn/teach myself php; ongoing process.
Indeed, I found out the best way to learn and put into practice PHP was to install a web server on my own computer (rather than relying on the upload of the modified templates to my ISP) such as XAMPP or Abyss web servers.  ;)
R_o_B
---
eMail: weather@herray.net

Offline Jáchym

  • Meteotemplate Developer
  • Forecaster
  • *****
  • Posts: 8605
    • Meteotemplate
Re: file_get_contents
« Reply #15 on: June 04, 2017, 08:33:32 PM »
Just so that you know, active support for version 5.6 of PHP ended on January 19, 2017 - only critical security issues will be fixed. The end of life for version 5.6 of PHP will be on December 31, 2018.

Quote
I'm trying to learn/teach myself php; ongoing process.
Indeed, I found out the best way to learn and put into practice PHP was to install a web server on my own computer (rather than relying on the upload of the modified templates to my ISP) such as XAMPP or Abyss web servers.  ;)

Yes... and you would be surprised how many people still use 5.4 and some were even complaining when I changed the minimum version supported by my template to 5.4 because they were still using 5.3...

Offline Otis

  • (aka Paul)
  • Forecaster
  • *****
  • Posts: 728
    • Lake Huron Weather
Re: file_get_contents
« Reply #16 on: June 05, 2017, 12:22:30 PM »
My hosting company solved the issue with PHP 7, they had to allow the subject command (allow_url_fopen = off).  They are not sure why I couldn't modify the php.ini file; they are looking into it.

So, I am back to PHP 7.x and site is working as it did.
Life is good.

CW3699