REST callback with PHP not working?

153 views
Skip to first unread message

Marcus Gerards

unread,
Apr 6, 2012, 2:28:36 PM4/6/12
to KarotzDev
Hi all!

I've been struggling with the REST-API for days now. :-(

Basically my code works like this: I create an array with multiple
pairs of actions and data, like "say" and "text-to-say" or "play" and
"URL to play". The goal is to make Karotz perform each action one
after another without any use of wait-functions like sleep();.

From what I understood of the API-Docs and references on this group,
it should work like this:

1. Initiate a callback and pass an interactiveID. Start interactive
mode
2. On the next callback, call the API to perform an action (e.g. "say
something")
3. On the next callback, check if the last action was cancelled or
terminated. If yes, call API for the next action, if no - quit.
4. repeat 3. untill actions are performed by Karotz
5. Stop interactive mode.


My code should do this:

1st callback (triggered by karotz.com-schedule or nanoz):
- set up the array (works)
- GET interactive id and store it in SESSION-variable (works)
- start interactive mode (works sometimes)
- quit

2nd callback:
- retrieve interactive ID from VoosMsg (works)
- restore SESSION (works, session-id == interactiveID)
- check for VoosMsg->event->code==(TERMINATED || CANCELLED) (works)
- if last action isn't finished, exit and wait for next callback
- if actions are left in the array, pull the next action and send
request to API (works sometimes)

...

nth callback
- stop interactive mode.

My code worked once (and only once!), but I can't get it to work
again. There may be bugs left or the API is just very unreliable, I
don't know. The requests results in 502-Errors most of the time.

Here is my code (the part with the SESSION and the actions works fine
in a karotz-less testscript):

<?php
if($_GET["interactiveid"]) {
session_id($_GET['interactiveid']); // session-name = interactiveID
(so we can reload the data once the Karotz-servers call us again
session_start();

//first callback start interactive mode
$_SESSION['interactiveid'] = $_GET["interactiveid"]; // store
interactiveID

// Set up the action-array
$actions = array(
array("say"=>"tralala"),
array("play"=>urlencode(<somemp3>)),
array("say"=>"bah")
);
$actions = array_reverse($actions);
$_SESSION['actions'] = $actions;
$_SESSION['counter'] = count($actions)+1;

$karotz_apipath = "http://api.karotz.com/api/karotz/";
$karotz_action_play = "multimedia?action=play&url=";
$karotz_action_say = "tts?action=speak&lang=DE&text=";
$karotz_interactive = "&interactiveid=" .
$_SESSION['interactiveid'];

$result= file_get_contents($karotz_apipath . $karotz_action_say .
"start" .$karotz_interactive); // say "Start" so the API might call
back
session_write_close();
exit(0);

}
else {

$data = file_get_contents("php://input");

//posted event


try {
$voosMsg =simplexml_load_string($data);

$interactiveId = (string) $voosMsg->interactiveId;
session_id($interactiveId); // pick up the session-
cookie
session_start();

$code = (string) $voosMsg->event->code;


if (isset($code) && isset($interactiveId)) {


if(($code == "TERMINATED") || ($code == "CANCELLED")) { // last
action finished?
$_SESSION['counter'] = $_SESSION['counter']-1;

$karotz_apipath = "http://api.karotz.com/api/karotz/";
$karotz_action_play = "multimedia?action=play&url=";
$karotz_action_say = "tts?action=speak&lang=DE&text=";
$karotz_interactive = "&interactiveid=" .
$_SESSION['interactiveid'];


if ($_SESSION['counter'] != 0) { // any actions left?
$counter = 1;

foreach ($_SESSION['actions'] as $param) { // grab
the next action
if ($counter == $_SESSION['counter']) {
$action = array_keys($param);
$parameter = array_values($param);

//
generate the API-call

if ($action[0] =="say") {
$result= file_get_contents($karotz_apipath .
$karotz_action_say . urlencode($parameter[0] .$karotz_interactive));
}
elseif ($action[0] =="play") {
$result= file_get_contents($karotz_apipath .
$karotz_action_play . urlencode($parameter[0] .$karotz_interactive));
}

session_write_close();
exit(0);
} else {
$counter++;
}
}
} else { // no more actions? delete session and stop
Karotz
$_SESSION = array();
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000, $params["path"],
$params["domain"], $params["secure"], $params["httponly"]
);
}
session_destroy();
file_get_contents("http://api.karotz.com/api/karotz/
interactivemode?action=stop&interactiveid=" . $interactiveId);
}
}
}
}
catch (Exception $e) {
echo $h . 'Exception : ' . $e->getMessage() . "\n";
}


I hope someone of you can help me to get this working. Implementing
this on a Nabaztag:tag took me 10(!) minutes - on the Karotz, it's a
nightmare.

Best wishes,

Marcus


vincent...@gmail.com

unread,
Apr 6, 2012, 3:00:27 PM4/6/12
to karo...@googlegroups.com
Hello Markus

I'm still working for such a projet.
I posted on this group a few days ago.
You can see bellow what I'm trying to do.

I bild a mini php class .
I an array I put the actions to do, with a free space for the correlation
id.
I send the first command put tuhe correlation id in the free space and I
serialize the class with the interid as name.
When I receive a callback from the api, I get the interid and deserialize
the class.
If received terminated, I drop the element of the correspodant correlation
id and send the next command etc...

I think where working on the same project ;-)

But I would like to be able to put actions to do from conditions of the
previous callback.
But I do not know how to do ...


-----Message d'origine-----
De : karo...@googlegroups.com [mailto:karo...@googlegroups.com] De la
part de Marcus Gerards
Envoyé : vendredi 6 avril 2012 20:29
À : KarotzDev
Objet : [karotzdev] REST callback with PHP not working?

Marcus Gerards

unread,
Apr 7, 2012, 1:41:58 PM4/7/12
to karo...@googlegroups.com
Hi Vince!


On Friday, 6 April 2012 21:00:27 UTC+2, Vince wrote:


I send the first command put tuhe correlation id in the free space and I
serialize the class with the interid as name.


Your remark about the correlationId was most helpful - the violet servers request the callback multiple times but do not always deliver a correlationId - I did not know this and it broke my code, because on every request one action was skipped. I check for the correlationId know and added the line 

if (empty($correlationId)) exit(0);

to my callback-code, which makes my script ignore any request without a correlationId. Now everything works as intended, thank you very much! Karotz performs each action from the predefined array one after another, without stopping or skipping a single one.

I'll clean up my code and post it here and on the wiki as well. 

I think where working on the same project ;-) 

But I would like to be able to put actions to do from conditions of the

previous callback.
But I do not know how to do ...

What conditions exactly? 

Best wishes,

Marcus

Gianfranco Panico

unread,
May 29, 2013, 3:26:12 PM5/29/13
to karo...@googlegroups.com, marcus....@googlemail.com
Hello Marcus
did you make your script done?
i would like to send play an mp3 stored on the web with an http command like i do with Nabaztag.
your app should be useful for me? 
Reply all
Reply to author
Forward
0 new messages