-2

I modified it all now I have this file that makes my api work.

auth.php:

<?php
include 'Unirest.php';



function login()
{

    $headers = array('Accept' => 'application/json');
    $data = array(
        "grant_type" => "password",
        "client_id" => "myclientid",
        "client_secret" => "myclientsecret",
        "username" => "username",
        "password" => "password"
    );

    $response = Unirest\Request::post('http://i-scent.fr/api/oauth_token', $headers, $data);
    //    $response->code;
    //    $response->headers;
    return $response->body->access_token;
}

function device_info($device_id,$token){

    $header = array('Accept' => 'application/json', 
                    'Authorization' => 'Bearer '.$token );
    $response = Unirest\Request::get('http://i-scent.fr/api/devices/'.$device_id,$header);
    echo $response->body->name;
    echo "</br>";

}

function diffuse($device_id,$token,$duration,$intensity){

    $header = array('Accept' => 'application/json', 'Authorization' => 'Bearer '.$token );
    $data = array('time' => 1, 'percent' => 50);

    $body = Unirest\Request\Body::form($data);


    $response = Unirest\Request::put('http://i-scent.fr/app_dev.php/api/device/'.$device_id.'/actions/diffusion',$header,$body);
    echo $response->code;
    echo "</br>";


}

When I use all the functions in a simple script it works perfectly on my website. But when I put it like this in my webhook, I have error 500 internal server error. I have all the unirest libraries.

<?php
include "auth.php";

function processMessage($update) {
    if($update["result"]["action"] == "sayHello"){

        $token = login();
        $name = device_info("1966",$token);
        diffuse("1966",$token,"0.5","50");

        sendMessage(array(
            "source" => $update["result"]["source"],
            "speech" => "bonjour webhook",
            "displayText" => "bonjour webhook",
            "contextOut" => array()
        ));

    }
}

function sendMessage($parameters) {
    echo json_encode($parameters);
}


$update_response = file_get_contents("php://input");
$update = json_decode($update_response, true);
if (isset($update["result"]["action"])) {

    processMessage($update);
}

Error 500 is supposed to mean that the webhokk's script crashed somewhere but I don't know where and why.

1 Answers1

2

Update 2

Based on your most recent code, you're including "auth.php", which works in the original environment (which is being called as part of a web page, it sounds like).

Your code has two functions, device_info() and diffuse(), which output their results instead of returning them. This output isn't JSON, and includes HTML markup. This is being sent as part of the result of your webhook and will cause what is returned to be invalid.

Update

Based on your latest code, there are still many logical, and a few syntactical, problems.

A "500 Internal Server Error" indicates that your program didn't run correctly and crashed for some reason. As posted, it is missing a closing }, which could be the problem if that isn't in your actual code.

Even if you fix that, there are many issues with the code:

  • It isn't clear what you intend to do with the results of calling your "test1" script. You store them in $data and don't do anything with it.

  • You're calling the other website (test1) before you look at what the user has asked you to do. Which is fine, but then why do you care what the user is asking you?

Original Answer

There are a few errors here, but the underlying problem is that you're mixing up where things run and the capabilities of the caller to your webhook.

For a Dialogflow webhook, Google/Dialogflow is sending JSON (which you seem to be handling ok), and expecting back JSON. Although it looks like you send this back as part of send_message(), you're also sending something back when you call connexion(). What you're sending back in this case is not JSON, but HTML with JavaScript.

Which leads to the second problem - If this was php that was generating an HTML page that included a script, you'd be in fine shape. But it isn't. You have to send back only JSON.

You can do something like this to call the other API and get back the contents:

$body = file_get_contents("http://google-home.exhalia.fr/test1");

Which will set $body to the body of the page you've called. What you do with that, at that point, is up to you. But you need to make this call before your call to send_message() because you want to represent the contents as part of what you're saying.

(See How to send a GET request from PHP? for a discussion of other methods available to you in case you need to do a POST, use header information, etc.)

Prisoner
  • 49,922
  • 7
  • 53
  • 105
  • I've updated my answer, but I don't know what you mean by "I don't want to get the content of index.php except if I can run it like that." – Prisoner Nov 24 '17 at 15:26
  • I changed it to `$body = file_get_contents("http://google-home.exhalia.fr/test1/index.php");` as you said. I deleted the function connexion and I've put this at the very beginning of my code. In test1 folder there is index.php that just runs different api to log, get informations on devices and send orders. What I want is just to call this script that already works well. It doesn't display or return anything. – erwan macé Nov 24 '17 at 15:37
  • Well, it sounds like you're running that script. This doesn't seem like a very useful experience, however, if it takes no input and returns no output. – Prisoner Nov 24 '17 at 17:45
  • Still not working with this code I just edited in the post. I don't understand why. – erwan macé Nov 27 '17 at 08:17
  • Updated answer again (put it on top) – Prisoner Nov 27 '17 at 14:04
  • I deleted every outputs but it didn't change anything. The problem is still here. – erwan macé Nov 27 '17 at 14:22
  • Nevermind, problem was on the server. Curl was missing it works now. Thanks for help. – erwan macé Nov 27 '17 at 14:43