Can't get Octopus API to work

I’m trying to write a PHP program to optimise my use of power and minimise costs using the Octopus Agile tariff. When I do the following:

|||$ch = curl_init();|
|---|---|---|
|||$from = date(Y-m-d, time() - 60 * 60 * 24) . T23:00:00Z;|
|||$to = date(Y-m-d, time() + 60 * 60 * 24) . T22:59:00Z;|
|||$url = https://api.octopus.energy/v1/electricity-meter-points/$mpan/meters/$meter_serial/consumption/?apikey=$api_key&period_from=$from&period_to=$to;|
|||curl_setopt($ch,CURLOPT_URL, $url);|
|||curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds|
|||curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);|
|||curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);|
|||curl_setopt($ch, CURLOPT_USERPWD, $api_key:);|
|||$consump = curl_exec($ch);|
|||$errno = curl_getinfo($ch, CURLINFO_OS_ERRNO);|
|||curl_close($ch);|

$errno = 0 and $consump = false.

What I intend is that $consump is the JSON data which I can concvert to an array using json_decode().

What am I doing worng? How can I investigate this further?

Thank you - Rowan

It can be very frustrating when these things don’t actually generate anything. I have never worked with PHP on this (I use Node-RED and it all works fine) however…

As a starting point it is worth building a complete URL with API key, mpan, meter number, and ? parameters in a text editor and pasting this into a browser - if correct it will return the results.

The URL base I use is

https://{{apikey}}:@api.octopus.energy/v1/electricity-meter-points/{{mpan}}/meters/{{meter}}/consumption

I can’t comment on how PHP manages the authentication, but this format works for me.

If the authentication is OK, and the URL has the correct mpan and meter, then a lack of results will be down to having a date issue.

The dates do not have to be specified - so start with zero parameters and see what comes back (the most recent 24 hours worth). After that add in the date parameters you want, but take note:

  • the consumption file has the most recent readings at the ‘front’, and by default you should get back the last 24 hours (48 readings) on file
  • readings are added during the day after, so electricity usually appears by 10:00 in the morning. Gas can take to mid afternoon to arrive, so asking for ‘todays’ meter readings will always end in nothing - you can only get ‘yesterday’ and older.
  • Dates must be correct ISO format. Again I have no idea what PHP is doing here, but if you are generated 2-digit years then it will not work. 2024-03-04T23:00:00Z is required.

I hope that has given you some useful pointers!