Author Topic: PHP script - Get the latest BloomSky image  (Read 19383 times)

0 Members and 1 Guest are viewing this topic.

Offline Jáchym

  • Meteotemplate Developer
  • Forecaster
  • *****
  • Posts: 8605
    • Meteotemplate
PHP script - Get the latest BloomSky image
« on: March 05, 2016, 09:54:35 AM »
Hi guys,
I have seen several people already asking about how to get the latest BloomSky image. Since it is a dynamic URL it is not as easy, so I created a little script which will do it for you.

Instructions:
1. Download the attached PHP file
2. Open the file in a text editor and insert your API key - if you don't have one, you need to get one for free from the official BloomSky developer's site
3. Upload the file anywhere on your server
4. For the image source on your page, use this php file.

eg:
<img src="...../bloomSkyLatest.php">

It should work, but since I cannot test properly because I dont have the camera, I would appreciate if someone could confirm it.

Enjoy
« Last Edit: March 05, 2016, 10:01:55 AM by Jachym »

Offline Bob3rd

  • Member
  • *
  • Posts: 35
Re: PHP script - Get the latest BloomSky image
« Reply #1 on: March 05, 2016, 10:06:19 AM »
I see two zipped PHP files with the same name.  For some reason I am unable to download either of them even though I am logged in to the forum. Perhaps it is a forum issue. .....(update)...now it works.  Thanks.  I am going to tinker with it a bit now.

Online PaulMy

  • Forecaster
  • *****
  • Posts: 5509
    • KomokaWeather
Re: PHP script - Get the latest BloomSky image
« Reply #2 on: March 05, 2016, 10:13:10 AM »
Funny, just last night I looked if I could do something like that - but I couldn't :oops:
 
Have downloaded the zip (I think the latest when only one was showing) and get this http://www.komokaweather.com/bloomsky/bloomSkyLatest.php
 
Did I miss something
 
Paul

Offline Jáchym

  • Meteotemplate Developer
  • Forecaster
  • *****
  • Posts: 8605
    • Meteotemplate
Re: PHP script - Get the latest BloomSky image
« Reply #3 on: March 05, 2016, 10:17:50 AM »
I checked the link Paul and I see an image of your snowy street, so I dont see any problems with it

Online PaulMy

  • Forecaster
  • *****
  • Posts: 5509
    • KomokaWeather
Re: PHP script - Get the latest BloomSky image
« Reply #4 on: March 05, 2016, 10:23:43 AM »
Hmm, with that link I get
Quote
<br /> <b>Warning</b>:  file_get_contents(https://api.bloomsky.com/api/skydata): failed to open stream: HTTP request failed! HTTP/1.0 401 Unauthorized in <b>/home/content/96/5379896/html/bloomsky/bloomSkyLatest.php</b> on line <b>22</b><br /> <br /> <b>Warning</b>:  file_get_contents(): Filename cannot be empty in <b>/home/content/96/5379896/html/bloomsky/bloomSkyLatest.php</b> on line <b>28</b><br /> 

At least you know the script is correct.  Now to find what is different for me ](*,)
 
Regards,
Paul

Offline Bob3rd

  • Member
  • *
  • Posts: 35
Re: PHP script - Get the latest BloomSky image
« Reply #5 on: March 05, 2016, 10:40:14 AM »
I'm getting an error message too.  My link is http://bbbrown.com/pages/bloomSkyLatest.php .  I used 
Code: [Select]
<img src="http://bbbrown.com/pages/bloomSkyLatest.php"></img> in my web page.  I put my API in the script in the appropriate place I think,  Could this be a conflict with Wordpress?
« Last Edit: March 05, 2016, 10:48:53 AM by Bob3rd »

Offline Jáchym

  • Meteotemplate Developer
  • Forecaster
  • *****
  • Posts: 8605
    • Meteotemplate
Re: PHP script - Get the latest BloomSky image
« Reply #6 on: March 05, 2016, 10:47:15 AM »
Strange...


Offline wvdkuil

  • Wim van der kuil
  • Forecaster
  • *****
  • Posts: 1986
    • My PWS at Leuven Belgium Europe
Re: PHP script - Get the latest BloomSky image
« Reply #7 on: March 05, 2016, 10:48:18 AM »
I'm getting an error message too.  My link is http://bbbrown.com/pages/bloomSkyLatest.php .  I used 
Code: [Select]
<img src="http://bbbrown.com/pages/bloomSkyLatest.php></img> in my web page.  I put my API in the script in the appropriate place I think,  Could this be a conflict with Wordpress?
There is  an " missing after  the link in your example code
Code: [Select]
<img src="http://bbbrown.com/pages/bloomSkyLatest.php"></img>Check if that is there, it should be.
Wim

Offline Jáchym

  • Meteotemplate Developer
  • Forecaster
  • *****
  • Posts: 8605
    • Meteotemplate
Re: PHP script - Get the latest BloomSky image
« Reply #8 on: March 05, 2016, 10:48:24 AM »
I'm getting an error message too.  My link is http://bbbrown.com/pages/bloomSkyLatest.php .  I used 
Code: [Select]
<img src="http://bbbrown.com/pages/bloomSkyLatest.php></img> in my web page.  I put my API in the script in the appropriate place I think,  Could this be a conflict with Wordpress?

You are missing the end quotation mark in your src

<img src="http://bbbrown.com/pages/bloomSkyLatest.php"></img>

Offline Bob3rd

  • Member
  • *
  • Posts: 35
Re: PHP script - Get the latest BloomSky image
« Reply #9 on: March 05, 2016, 10:49:58 AM »
Sorry.  That was a typo when I was creating the post.  The end quote is there on the page.

Offline Jáchym

  • Meteotemplate Developer
  • Forecaster
  • *****
  • Posts: 8605
    • Meteotemplate
Re: PHP script - Get the latest BloomSky image
« Reply #10 on: March 05, 2016, 10:54:48 AM »
Hmm I see two possibilities:

- your server does not allow external file_get_contents commands
- your API key is not working

You can PM me your API and I will test it on my server if you want

Offline Bob3rd

  • Member
  • *
  • Posts: 35
Re: PHP script - Get the latest BloomSky image
« Reply #11 on: March 05, 2016, 10:58:59 AM »
PM sent.

Offline Jáchym

  • Meteotemplate Developer
  • Forecaster
  • *****
  • Posts: 8605
    • Meteotemplate
Re: PHP script - Get the latest BloomSky image
« Reply #12 on: March 05, 2016, 11:01:49 AM »
Yes, I just tried and it works on my end.

Lets take it step by step,
try using this script first, let me know if it helped:
Code: [Select]
header('Content-type: image/png');

$APIkey = "";
$url = "https://api.bloomsky.com/api/skydata";

$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Authorization: ".$APIkey."\r\n"
)
);
$context = stream_context_create($opts);
$file = curl_get_contents($url, false, $context);

$data = json_decode($file, true);

$imageURL = $data[0]['Data']['ImageURL'];

echo curl_get_contents(  $imageURL );

function curl_get_contents($url){
$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_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;
}

Offline wvdkuil

  • Wim van der kuil
  • Forecaster
  • *****
  • Posts: 1986
    • My PWS at Leuven Belgium Europe
Re: PHP script - Get the latest BloomSky image
« Reply #13 on: March 05, 2016, 11:08:52 AM »
@jachym @Bob3rd @PaulMy
I enclose the smallest testscript to see what the errors are.
As the browser call works the error is in his script to load the image @Bob3rd, same as for @PaulMy

If they use this => and the image displays then they should check their scripts with the <img /> tag again.

Code: [Select]
<?php ini_set('display_errors''On');   error_reporting(E_ALL);
?>

<img src="./bloomSkyLatest.php">

Wim


Offline Bob3rd

  • Member
  • *
  • Posts: 35
Re: PHP script - Get the latest BloomSky image
« Reply #14 on: March 05, 2016, 11:11:32 AM »
Okay.  I placed it at http://bbbrown.com/weather/bloomsky-php-script-test/ .  The page loads with no error but there is a ghost image icon instead of the real thing.

Offline Jáchym

  • Meteotemplate Developer
  • Forecaster
  • *****
  • Posts: 8605
    • Meteotemplate
Re: PHP script - Get the latest BloomSky image
« Reply #15 on: March 05, 2016, 11:14:05 AM »
Thanks Wim,

in Paul's case though I dont understand the problem because it works when I try his link, I dont understand how can he get a different output from the server when he loads it in his browser... in Bob's case it obviously is due to the server configuration, but how can Paul's link work in my browser and not his. ](*,)

Offline wvdkuil

  • Wim van der kuil
  • Forecaster
  • *****
  • Posts: 1986
    • My PWS at Leuven Belgium Europe
Re: PHP script - Get the latest BloomSky image
« Reply #16 on: March 05, 2016, 11:19:16 AM »
Thanks Wim,

in Paul's case though I dont understand the problem because it works when I try his link, I dont understand how can he get a different output from the server when he loads it in his browser... in Bob's case it obviously is due to the server configuration, but how can Paul's link work in my browser and not his. ](*,)
When these scripts are called in the browser,
http://bbbrown.com/pages/bloomSkyLatest.php
http://www.komokaweather.com/bloomsky/bloomSkyLatest.php
they are executed on their server.  They run Ok, so your script is OK.

When there is an error it is in their script with the <img tag,  it is inside their script, not inside your script
Wim

Offline wvdkuil

  • Wim van der kuil
  • Forecaster
  • *****
  • Posts: 1986
    • My PWS at Leuven Belgium Europe
Re: PHP script - Get the latest BloomSky image
« Reply #17 on: March 05, 2016, 11:23:01 AM »
Okay.  I placed it at http://bbbrown.com/weather/bloomsky-php-script-test/ .  The page loads with no error but there is a ghost image icon instead of the real thing.
What did you place where.
The test-img.php script should be in the same folder as the bloomSkyLatest.php script.
Just to see the errors when executing.
Wim

Offline Bob3rd

  • Member
  • *
  • Posts: 35
Re: PHP script - Get the latest BloomSky image
« Reply #18 on: March 05, 2016, 11:23:57 AM »
I think I will contact my hosting service to find out if they support PHP and which version they support.  When I ran your original script just by pointing my browser directly to the file, I got an error that said it could not load the image because the image contained errors.  That was the closest to success I got.

Offline Bob3rd

  • Member
  • *
  • Posts: 35
Re: PHP script - Get the latest BloomSky image
« Reply #19 on: March 05, 2016, 11:24:49 AM »
Okay.  I placed it at http://bbbrown.com/weather/bloomsky-php-script-test/ .  The page loads with no error but there is a ghost image icon instead of the real thing.
What did you place where.
The test-img.php script should be in the same folder as the bloomSkyLatest.php script.
Just to see the errors when executing.
Wim
Oh. sorry I didn't know that.  I'll move it and try again.

Online PaulMy

  • Forecaster
  • *****
  • Posts: 5509
    • KomokaWeather
Re: PHP script - Get the latest BloomSky image
« Reply #20 on: March 05, 2016, 11:28:30 AM »
Thanks Wim,
Yes that test-img.php did work and shows the BloomSky latest.  I then added your suggested code in my www.komokaweather.com page and it displays there now.
 
Before I added that code I did enter that URL www.komokaweather.com/bloomsky/bloomSkyLatest.php and then it also displayed.  Earlier I had tried it a dozen or more times all with that same error message.  Maybe a cache thing or something.  But all looks good now.
 
Again thanks Wim, and Jachym.
 
Paul
 

Offline ke9lz

  • De Pere - Athelstane Weather
  • Contributor
  • ***
  • Posts: 143
    • De Pere Weather
Re: PHP script - Get the latest BloomSky image
« Reply #21 on: March 05, 2016, 11:36:23 AM »
Hi Jachym,  Thanks for sharing the script!  I have been trying to figure a way to just get the current image using web cam software and this works great!  I put your script on my web server and then using Imagesalsa I call that script to get the latest image.  From there I can manipulate the image and then upload it up to may server.  Here is the result http://weather.athelstanewi.com/wxbloom.php.  Now I can add it along with my other webcam images.  Cool stuff!
Steve (KE9LZ)
Athelstane Weather
Blitzortung 1177, 1565


Offline Bob3rd

  • Member
  • *
  • Posts: 35
Re: PHP script - Get the latest BloomSky image
« Reply #22 on: March 05, 2016, 11:37:19 AM »
If I point my browser directly to http://www.bbbrown.com/test-image.php the text of the script is returned to me.  I'm not a programmer so it might be me causing the problem.
« Last Edit: March 05, 2016, 11:38:57 AM by Bob3rd »

Offline Bob3rd

  • Member
  • *
  • Posts: 35
Re: PHP script - Get the latest BloomSky image
« Reply #23 on: March 05, 2016, 11:42:56 AM »
Just to recap.  I edited the original BloomskyLatest.php and added my API.  It didn't work.  I sent you my API key and it worked using your server.

You sent the minimal test-image.php script which I placed in the same folder as the above script.  When I point my browser to that I only get the text of the script returned.  Probably because I don't know what I am doing.

Offline wvdkuil

  • Wim van der kuil
  • Forecaster
  • *****
  • Posts: 1986
    • My PWS at Leuven Belgium Europe
Re: PHP script - Get the latest BloomSky image
« Reply #24 on: March 05, 2016, 11:49:41 AM »
Just to recap.  I edited the original BloomskyLatest.php and added my API.  It didn't work.  I sent you my API key and it worked using your server.

You sent the minimal test-image.php script which I placed in the same folder as the above script.  When I point my browser to that I only get the text of the script returned.  Probably because I don't know what I am doing.
No sir, it is the strange behavior of some webservers and some assisting programs when uploading.
Please use a decent FTP program to upload both jachyms script and mine again.
Put the in a new folder to be sure that there is nothing else disturbing things.
Why? Because some extra html is enclosing the scripts. This is the html source of  http://www.bbbrown.com/test-image.php
Code: [Select]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta http-equiv="Content-Style-Type" content="text/css">
  <title></title>
  <meta name="Generator" content="Cocoa HTML Writer">
  <meta name="CocoaVersion" content="1404.34">
  <style type="text/css">
    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #454545}
  </style>
</head>
<body>
<p class="p1">&lt;?php ini_set('display_errors', 'On');   error_reporting(E_ALL);</p>
<p class="p1">?&gt;</p>
<p class="p1">&lt;img src="./bloomSkyLatest.php"&gt;</p>
</body>
</html>
So probably you are uploading this using a WYSIWG editor Cocoa HTML Writer.

You should ONLY upload the scripts by a plain FTP program.
Nothing else should be added.

Wim