Last active
December 15, 2015 02:49
-
-
Save t2-support-gists/5189860 to your computer and use it in GitHub Desktop.
SpeechCustom PHP RESTFul App 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AT&T API Platform Samples - SpeechCustom app 1 | |
------------------------------ | |
This file describes how to set-up, configure and run the PHP Applications | |
using AT&T API Platform services. | |
It covers all steps required to register the application and, | |
based on the generated API keys and secrets, create and run one's own | |
full-fledged sample applications. | |
1. Configuration | |
2. Installation | |
3. Parameters | |
1. Configuration | |
Configuration consists of a few steps necessary to get an application | |
registered with the proper services and | |
endpoints, depending on the type of client-side application | |
(autonomous/non-autonomous). | |
To register an application, go to https://developer.att.com/developer/mvc/auth/login and login | |
with your valid username and password. | |
Next, choose "My Apps" from the bar at the top of the page and click the | |
"Setup a New Application" button. | |
Fill in the form, in particular all fields marked as "required". | |
Having your application registered, you will get back an important pair of | |
data: an API key and Secret key. They are | |
necessary to get your applications working with the AT&T API Platform. See | |
'Adjusting parameters' below to learn how to use | |
these keys. | |
Initially your newly registered application is restricted to the "Sandbox" | |
environment only. To move it to production, | |
you may promote it by clicking the "Promote to production" button. Notice | |
that you will get a different API key and secret, | |
so these values in your application should be adjusted accordingly. | |
Depending on the kind of authentication used, an application may be based on | |
either the Autonomous Client or the Web-Server | |
Client OAuth flow (see https://developer.att.com/apis/oauth-2/docs). | |
2. Installation | |
** Requirements | |
Apache web server | |
PHP 5.4+ | |
Apache and PHP configured, on most GNU/Linux systems if installed using packages | |
this will be done automatically. | |
Copy the sample application folder to Apache web root folder, for example | |
/var/www/html. | |
3. Parameters | |
Each application contains a config.php file. This file holds configurable parameters described in an | |
easy-to-read format. Please modify the config.php file using the comments specified within the file. | |
Note: If your application is promoted from Sandbox environment to Production environment and you decide to use | |
production application settings, you must update parameters as per production application details. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$api_key = "4bf126e402d74a1e922a8c8bf256b2ac"; | |
$secret_key = "5b5e0d7003967dc6"; | |
$FQDN = "https://api.att.com"; | |
$scope = "STTC"; | |
$oauth_file = "oauthtoken.php"; | |
$speech_context_config = array('GenericHints', 'GrammarList'); | |
$default_file = "default.wav"; | |
$x_arg = "GrammarPenaltyPrefix=1.1,GrammarPenaltyGeneric=2.0,GrammarPenaltyAltgram=4.1"; | |
$xSpeechSubContext = 'Chat'; | |
$audioFolder = "audio"; | |
$linkSource = 'https://gist.github.com/5189860'; | |
$linkDownload = 'https://github.com/attdevsupport/ATT_APIPlatform_SampleApps/tree/master/RESTFul/SpeechCustom/PHP/app1'; | |
$linkHelp = 'https://raw.github.com/attdevsupport/ATT_APIPlatform_SampleApps/master/RESTFul/SpeechCustom/PHP/app1/README.txt'; | |
$endpoint = $FQDN . "/speech/v3/speechToTextCustom"; | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
session_start(); | |
require __DIR__ . '/config.php'; | |
require_once __DIR__ . '/src/Controller/SpeechController.php'; | |
require_once __DIR__ . '/lib/Util/Util.php'; | |
use Att\Api\Util\Util; | |
$controller = new SpeechController(); | |
$controller->handleRequest(); | |
$results = $controller->getResults(); | |
$errors = $controller->getErrors(); | |
?> | |
<!DOCTYPE html> | |
<!-- | |
Copyright 2014 AT&T | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
--> | |
<html lang="en"> | |
<head> | |
<title>AT&T Sample Speech Application - Speech to Text Custom</title> | |
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"> | |
<meta id="viewport" name="viewport" content="width=device-width,minimum-scale=1,maximum-scale=1"> | |
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> | |
<link rel="stylesheet" type="text/css" href="style/common.css"> | |
<script type="text/javascript"> | |
var _gaq = _gaq || []; | |
_gaq.push(['_setAccount', 'UA-33466541-1']); | |
_gaq.push(['_trackPageview']); | |
(function () { | |
var ga = document.createElement('script'); | |
ga.type = 'text/javascript'; | |
ga.async = true; | |
ga.src = ('https:' == document.location.protocol ? 'https://ssl' | |
: 'http://www') | |
+ '.google-analytics.com/ga.js'; | |
var s = document.getElementsByTagName('script')[0]; | |
s.parentNode.insertBefore(ga, s); | |
})(); | |
function enableNameParam(list,nameParam){ | |
var selectedValue = list.options[list.selectedIndex].value; | |
if(selectedValue == "GenericHints"){ | |
document.getElementById("nameParam").disabled=false; | |
}else{ | |
document.getElementById("nameParam").disabled=true; | |
var choices = document.getElementById("nameParam"); | |
choices.options[0].selected = true; | |
} | |
} | |
</script> | |
</head> | |
<body> | |
<div id="pageContainer" class="pageContainer"> | |
<div id="header"> | |
<div class="logo" id="top"></div> | |
<div id="menuButton" class="hide"> | |
<a id="jump" href="#nav">Main Navigation</a> | |
</div> | |
<ul class="links" id="nav"> | |
<li> | |
<a href="<?php echo $linkSource; ?>" | |
target="_blank">Source<img src="images/source.png" alt="Source" /></a> | |
<span class="divider"> | </span> | |
</li> | |
<li> | |
<a href="<?php echo $linkDownload; ?>" | |
target="_blank">Download<img src="images/download.png" alt="Link"></a> | |
<span class="divider">| </span> | |
</li> | |
<li><a href="<?php echo $linkHelp; ?>" target="_blank">Help</a></li> | |
<li id="back"><a href="#top">Back to top</a></li> | |
</ul> <!-- end of links --> | |
</div> <!-- end of header --> | |
<div class="content"> | |
<div class="contentHeading"> | |
<h1>AT&T Sample Application - Speech to Text Custom</h1> | |
<div id="introtext"> | |
<div><b>Server Time: </b><?php echo Util::getServerTime(); ?></div> | |
<div> | |
<b>Client Time: </b> | |
<script> | |
document.write("" + new Date()); | |
</script> | |
</div> | |
<div> | |
<b>User Agent: </b> | |
<script> | |
document.write("" + navigator.userAgent); | |
</script> | |
</div> | |
</div> <!-- end of introtext --> | |
</div><!-- end of contentHeading --> | |
<div class="formBox" id="formBox"> | |
<div id="formContainer" class="formContainer"> | |
<form name="SpeechToText" action="index.php" method="post"> | |
<div id="formData"> | |
<!-- start context select --> | |
<h3>Speech Context:</h3> | |
<select name="SpeechContext" onchange="enableNameParam(this,'nameParam')"> | |
<?php | |
$speechContexts = $controller->getSpeechContexts(); | |
foreach ($speechContexts as $sname) { | |
$selected = ''; | |
if ($controller->isSpeechContextSelected($sname)) $selected = 'selected '; | |
?> | |
<option <?php echo $selected; ?>value="<?php echo $sname ?>"><?php echo $sname ?></option> | |
<?php } ?> | |
</select> | |
<!-- end context select --> | |
<!-- name parameter select --> | |
<h3>Name Parameter:</h3> | |
<select name="nameParam" id="nameParam"> | |
<?php | |
$nameParams = array('x-grammar', 'x-grammar-prefix', 'x-grammar-altgram'); | |
foreach ($nameParams as $nameParam) { | |
if(isset($_SESSION['nameParam']) && $_SESSION['nameParam'] == $nameParam) { | |
?> | |
<option value="<?php echo $nameParam; ?>" | |
selected="selected"><?php echo $nameParam; ?></option> | |
<?php } else { ?> | |
<option value="<?php echo $nameParam; ?>"><?php echo $nameParam; ?></option> | |
<?php } ?> | |
<?php } ?> | |
</select> | |
<!-- end name parameter select --> | |
<!-- start audio file select --> | |
<h3>Audio File:</h3> | |
<select name="audio_file"> | |
<?php | |
$audioFiles = $controller->getAudioFiles(); | |
foreach ($audioFiles as $fname) { | |
$selected = ''; | |
if ($controller->isAudioFileSelected($fname)) $selected ='selected '; | |
?> | |
<option <?php echo $selected; ?>value="<?php echo $fname ?>"><?php echo $fname ?></option> | |
<?php } ?> | |
</select> | |
<!-- end audio file select --> | |
<h3>X-Arg:</h3> | |
<label><?php echo htmlspecialchars($x_arg); ?></label> | |
<h3>MIME Data</h3> | |
<textarea id="x_arg" name="x_arg" readonly="readonly" rows="4" | |
value="<?php echo htmlspecialchars($controller->getMIMEData()); ?>" | |
><?php echo htmlspecialchars($controller->getMIMEData()); ?></textarea> | |
<br> | |
<button type="submit" name="SpeechToText" id="SpeechToTextCustom">Submit</button> | |
</div> <!-- end of formData --> | |
</form> <!-- end of SpeechToText form --> | |
</div> <!-- end of formContainer --> | |
<?php | |
if (isset($errors[SpeechController::ERROR_STT])) { | |
$error = $errors[SpeechController::ERROR_STT]; | |
?> | |
<div class="errorWide"> | |
<strong>ERROR:</strong><br><?php echo htmlspecialchars($error); ?> | |
</div> | |
<?php } else if (isset($results[SpeechController::RESULT_STT])) { | |
$response = $results[SpeechController::RESULT_STT]; | |
?> | |
<div class="successWide"> | |
<strong>SUCCESS:</strong> <br>Response parameters listed below. | |
</div> | |
<table class="kvp"> | |
<thead> | |
<tr> | |
<th class="label">Parameter</th> | |
<th class="label">Value</th> | |
</tr> | |
</thead> | |
<tbody> | |
<?php | |
$recognization = $response['Recognition']; | |
$nbest = $recognization['NBest']; | |
$nbest = $nbest[0]; | |
?> | |
<tr> | |
<td class="cell" align="center"><em>ResponseId</em></td> | |
<td class="cell" align="center"><em><?php echo $recognization['ResponseId']; ?></em></td> | |
</tr> | |
<tr> | |
<td class="cell" align="center"><em>Status</em></td> | |
<td class="cell" align="center"><em><?php echo $recognization['Status']; ?></em></td> | |
</tr> | |
<tr> | |
<td class="cell" align="center"><em>Hypothesis</em></td> | |
<td class="cell" align="center"><em><?php echo $nbest['Hypothesis']; ?></em></td> | |
</tr> | |
<tr> | |
<td class="cell" align="center"><em>LanguageId</em></td> | |
<td class="cell" align="center"><em><?php echo $nbest['LanguageId']; ?></em></td> | |
</tr> | |
<tr> | |
<td class="cell" align="center"><em>Confidence</em></td> | |
<td class="cell" align="center"><em><?php echo $nbest['Confidence']; ?></em></td> | |
</tr> | |
<tr> | |
<td class="cell" align="center"><em>Grade</em></td> | |
<td class="cell" align="center"><em><?php echo $nbest['Grade']; ?></em></td> | |
</tr> | |
<tr> | |
<td class="cell" align="center"><em>ResultText</em></td> | |
<td class="cell" align="center"><em><?php echo $nbest['ResultText']; ?></em></td> | |
</tr> | |
<tr> | |
<td class="cell" align="center"><em>Words</em></td> | |
<td class="cell" align="center"><em><?php echo json_encode($nbest['Words']); ?></em></td> | |
</tr> | |
<tr> | |
<td class="cell" align="center"><em>WordScores</em></td> | |
<td class="cell" align="center"><em><?php echo json_encode($nbest['WordScores']); ?></em></td> | |
</tr> | |
</tbody> | |
</table> | |
<?php } ?> | |
</div> <!-- end of formBox --> | |
</div> <!-- end of content --> | |
<div id="footer"> | |
<div id="ft"> | |
<div id="powered_by">Powered by AT&T Cloud Architecture</div> | |
<p> | |
The Application hosted on this site are working examples intended to be used for reference in creating | |
products to consume AT&T Services and not meant to be used as part of your product. The data in | |
these pages is for test purposes only and intended only for use as a reference in how the services | |
perform. | |
<br><br> | |
To access your apps, please go to | |
<a href="https://developer.att.com/developer/mvc/auth/login" | |
target="_blank">https://developer.att.com/developer/mvc/auth/login</a> | |
<br> For support refer to | |
<a href="https://developer.att.com/support">https://developer.att.com/support</a> | |
<br> <br> | |
© 2014 AT&T Intellectual Property. All rights reserved. | |
<a href="http://developer.att.com/" target="_blank">http://developer.att.com</a> | |
</p> | |
</div> <!-- end of ft --> | |
</div> <!-- end of footer --> | |
</div> <!-- end of page_container --> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php /* | |
efb4edac6904c67468af4e2fd0f2c40e | |
0 | |
2dbe47168713a5d20a91e198d43970b7644e1549 | |
*/ ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment