How to use the API from a PHP script

I would like to update values in emoncms using the API.  Entering the following line into my browser works fine:

http://192.168.0.30/emoncms/input/post?json={boiler:1}

I am running a PHP script (not in a browser), and would like to  be able to update the values from within my script.  Can anyone provide some pointers as to how to do this please?

Many thanks in advance.

Nick

jonbev's picture

Re: How to use the API from a PHP script

Try file_get_contents("http://192.168.0.30/emoncms/input/post?json={boiler:1}")

php help is here - http://php.net/manual/en/function.file-get-contents.php

You probably need to add your apikey onto the url too 

file_get_contents("http://192.168.0.30/emoncms/input/post?json={boiler:1}&apikey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")

engeeaitch's picture

Re: How to use the API from a PHP script

That worked a treat thank you.

I now want to read data from emoncms from my php script.  Typing http://192.168.0.30/emoncms/feed/get?id=7 into the browser gives me the results, but in the php script, I get the data I want embedded within the html.

I can search for the text e.g. "id:" in the html, but this doesn't sound very future-proof.

Is there a better way please?  (I could just query the database, but going via the api should be the safer approach).

TrystanLea's picture

Re: How to use the API from a PHP script

Try:

http://192.168.0.30/emoncms/feed/get.json?id=7

The .json extension will not wrap the output in the full html theme. 

engeeaitch's picture

Re: How to use the API from a PHP script

Perfect.  Many thanks.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.