Hassio sensor state in php page

Good day, Could anyone with PHP knowledge assist me with getting a sensor value into a basic PHP page? I searched around and found some code which I will post below that I have added to a new index.php page but it seems to give me a 500 error.

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://hassio_IP:8123/api/states/sensor.mysensor');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: Bearer $MYTOKEN'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
echo '<br>';

$info = json_decode($result,true);

$entity_id = $info['entity_id'];
$state = $info['state'];
$last_changed = $info['last_changed'];
echo $info.'  '.$state.'  '.$last_changed;
?>
1 Like