This is the forum archive of Homey. For more information about Homey, visit the Official Homey website.

The Homey Community has been moved to https://community.athom.com.

This forum is now read-only for archive purposes.

Speech by PHP CURL

Michiel145Michiel145 Member
edited July 2017 in Developers
Hi there,

I'm playing around with the web-api and I'm able to readout devices, switch devices, but I cant seem to make Homey speak by CURL GET and PUT.

Is there a way to do this?
I see some topics where they say it should work so PUT to  http://HOMEYIP/api/manager/speech-output/, but is does not work. 
The error I get is "{"status":404,"result":"not_found"}".

When trying to PUT to /manager/speech-output/, I get "Cannot PUT /manager/speech-output/".  :/  

Thanks!

Some example PHP code

function speech_output($text){
 $apiUrl = 'http://'.HOMEY_IP.'/manager/speech-output/';

 $data = array("text" => $text);
 $data_json = json_encode($data);

 $curl = curl_init($apiUrl);
 $headers = array(
    'Authorization: Bearer '.HOMEY_TOKEN,
    'Content-Type: application/json',
 );
 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
 curl_setopt($curl, CURLOPT_POSTFIELDS,$data_json);
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
 $json = curl_exec($curl);

 echo $json;
 $arr_data = (json_decode($json, true));
 if($arr_data[status] <> "200"){ 
  return false;
 }else{ 
  return true;
 }
}


Comments

  • caspercasper Member
    edited July 2017
    Hi!

    The problem is that you're making a PUT request instead of a POST request ;).

    The following should work:

    $text = 'Hello, world!'; $curl = curl_init('http://YOUR-HOMEY-IP/api/manager/speech-output/'); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(['text' => $text])); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Authorization: Bearer YOUR-BEARER-TOKEN', ]); $result = curl_exec($curl); var_dump($result);
  • swtttswttt Member
    Indeed, PUT is for altering data. Like capabilities for devices etc. Where POST is used for adding stuff (in this case speech)
  • Works like a charm. Thank you guys!  :)
  • Michiel145Michiel145 Member
    edited July 2017
    Why did nobody developed a PHP class for this? Or is there one, and why are I'm re-inventing the wheel here?  :o
  • casedacaseda Member
    edited July 2017
    @Michiel145
    i didn't because the api changed too much (when i was working on it), and might still change a little :smile:

    plus i think you are the second one (i know of) that works with PHP for homey, the rest works mostly with javascript (and modules)

    even though i also started in PHP, i switched to javascript (ajax) since that was easier to do anything without having to refresh pages, making the pages more dynamic with showing information
  • technimantechniman Member
    edited July 2017
    3rd, I also us PHP for basically everything outside of Homey :-)

    The thing i don't get (and am looking for) is to "Send" text to Homey, who will pick this up as if it was spoken,
    changing the URL in the above script form "speech-output" to "speech-input" did not work for some reason..
    It shows  :  "{"status":500,"result":"invalid_transcript"}"

    -= edit =-  
    sorted, though more testing is needed, in the above code change
    <code>
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(['text' => $text]));
    </code>
    to
    <code>
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(['transcript' => $text]));
    </code> 

    (obviously also change the URL to speech-input)

    Now I can finally use Autovoice to control Homey

    -=edit2 =- 

    Dear lord, this works really well .. 
Sign In or Register to comment.