Calling Yahoo Weather API from VoiceXML - api

I'm trying to make a voice weather system with voiceXML and the Yahoo Weather API. To develop my program I'm using voxeo evolution.
To call the Weather API I'm using the data vxml tag with an srcexpr because I need a dynamic URL (the program asks the user for a city to check weather in).
Here is my code:
<?xml version="1.0" encoding="UTF-8"?>
<vxml version = "2.1">
<form id="mainMenu">
<field name="City">
<prompt>
Please, name a spanish city.
</prompt>
<grammar src="city.grammar"/>
</field>
<!-- code taken from the javascript example of the yahoo weather api -->
<script>
<![CDATA[
var callbackFunction = function(data) {
var wind = data.query.results.channel.wind;
alert(wind.chill);
};
]]>
</script>
<data srcexpr="https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places where text='"+City+", spain')&callback=callbackFunction"/>
</form>
</vxml>
The program doesn't work because of the data tag to connect to the weather API, but I don't know why. Do someone know why is failing?

I finally solved my problem making a php script to connect to the yahoo api and calling it using submit tag in VoiceXML.
<?php
$City = $_REQUEST["City"];
$Day = $_REQUEST["Day"];
$BASE_URL = "http://query.yahooapis.com/v1/public/yql";
$yql_query = 'select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="('.$City.', spain)") and u="c"';
$yql_query_url = $BASE_URL . "?q=" . urlencode($yql_query) . "&format=json";
$session = curl_init($yql_query_url);
curl_setopt($session, CURLOPT_RETURNTRANSFER,true);
$yahooapi = curl_exec($session);
$weather = json_decode($yahooapi,true);
$weather_resumen = $weather['query']['results']['channel']['item']['forecast'];
$weather_today = $weather_resumen[0]['day'];
// yahoo api returns an array with the weather for the next week ordered by
// day (0 -> today, 1 -> tomorrow...). Function get_day gets the index of
// the day the user said
$index_weather = get_day($weather_today, $Day);
$condition_index = $weather_resumen[$index_weather]['code'];
$weather_condition = $cond_met[intval($condition_index)];
$min_temp = $weather_resumen[$index_weather]['low'];
$max_temp = $weather_resumen[$index_weather]['high'];
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
?>
<vxml version="2.1" xml:lang="es-ES">
<form id="form_main">
<block>
<prompt>
The weather for <?php echo $Day ?> in <?php echo $City ?> is <?php echo $weather_condition ?>. The lowest temperature will be <?php echo $min_temp ?> and the highest <?php echo $max_temp ?>.
<break/>
</prompt>
</block>
</form>
</vxml>

Related

Magento 2 : How to display customer email on checkout success page?

My code is given below
app/code/custom/checkout/block/success.php
class Success extends \Magento\Checkout\Block\Onepage\Success
{
public function getOrder()
{
$order = $this->_checkoutSession->getLastRealOrder();
return $order;
}
app/design/frontend/vendor/themename/Magento_Checkout/layout/checkout_onepage_success.xml
<referenceContainer name="content">
<block class="Custom\Checkout\Block\Success" name="checkout.success" template="Magento_Checkout::success.phtml" cacheable="false">
<container name="order.success.additional.info" label="Order Success Additional Info"/>
</block>
</referenceContainer>
app/design/frontend/vendor/themename/Magento_Checkout/templates/success.phtml
<?php if($block->getOrderId):?>
<div class="success-title" data-bind="i18n: 'Thank you for your purchase!'" data-role="title">
<h2><?= __('Thank you for your purchase!');?></h2>
</div>
<p><?= __('Your payment has been received. A confirmation of your order has been sent to ');?><?php echo '"'.$block->getOrder()->getCustomerEmail().'"'?></p>
<?php if ($block->getCanViewOrder()) :?>
<p class="successmessge"><?= __('Your order number is: %1.', sprintf('<strong>%s</strong>', $block->escapeHtml($block->getViewOrderUrl()), $block->escapeHtml($block->getOrderId()))) ?></p>
<?php else :?>
<p class="successmessge"><?= __('Your order number is: <span>%1</span>.', $block->escapeHtml($block->getOrderId())) ?></p>
<?php endif;?>
<p><?= /* #escapeNotVerified */ __('Check your inbox for estimated delivery time') ?></p>
<p><?= /* #escapeNotVerified */ __('If you have any questions regarding your order , please contact our customer service department on contact#cii.co.uk or +44(0)12 3456 7891') ?></p>
<?php endif;?>
$block->getOrder()->getCustomerEmail();
this is returning error : call to non-member function is null
app/code/Custom/Checkout/etc/frontend/di.xml
<preference for="Custom\Checkout\Block\Success" type="Magento\Checkout\Block\Onepage\Success" />
All the things are working well.
but not getting block to success page
Please help me what is wrong in my code?

INSERT in the query part of BigQuery, using google-api-php-client

I wanted to insert thousands of records in the database using INSERT command in php script , as it will easier to access, but https://developers.google.com/bigquery/docs/query-reference , this doesn't show the INSERT command that can be used while querying in php , like ,
$quert->setQuery("Insert into ... values...") , Have tried this in the Query Table of BigQuery web console, but it doesn't seem to work , Is there anyway to use setQuery() with some other command, to insert data ?
BigQuery doesn't support the INSERT command. You would need to create a load job. See https://developers.google.com/bigquery/docs/import#localimport for more information.
In addition to Jordan's answer, here's a snippet of code that should get you started using the Google BigQuery API and the Google API PHP client library for loading your own data into BigQuery programmatically. Note, this snippet simply spits out the raw API response of the load job, including the job Id to the screen - you'll have to add your own polling logic to check on the load job status.
(We will be adding additional documentation about loading your own data, as well as more PHP samples soon).
<?php
require_once "google-api-php-client/src/Google_Client.php";
require_once "google-api-php-client/src/contrib/Google_BigqueryService.php";
session_start();
$client = new Google_Client();
// Visit https://code.google.com/apis/console to generate your
// oauth2_client_id, oauth2_client_secret, and to register your oauth2_redirect_uri.
$client->setScopes(array('https://www.googleapis.com/auth/bigquery'));
$client->setClientId('XXXXXXXXX.apps.googleusercontent.com');
$client->setClientSecret('XXXXXXXXX');
$client->setRedirectUri('http://YOURAPPLICATION/index.php');
// Instantiate a new BigQuery Client
$bigqueryService = new Google_BigqueryService($client);
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
?>
<!doctype html>
<html>
<head>
<title>BigQuery API Sample</title>
</head>
<body>
<div id='container'>
<div id='top'><h1>BigQuery API Sample</h1></div>
<div id='main'>
<?php
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if ($client->getAccessToken()) {
// Your project number, from the developers.google.com/console project you created
// when signing up for BigQuery
$project_number = 'XXXXXXXXXXXXXX';
// Information about the destination table
$destination_table = new Google_TableReference();
$destination_table->setProjectId($project_number);
$destination_table->setDatasetId('php_test');
$destination_table->setTableId('my_new_table');
// Information about the schema for your new table
$schema_fields = array();
$schema_fields[0] = new Google_TableFieldSchema();
$schema_fields[0]->setName('first');
$schema_fields[0]->setType('string');
$schema_fields[1] = new Google_TableFieldSchema();
$schema_fields[1]->setName('last');
$schema_fields[1]->setType('string');
$destination_table_schema = new Google_TableSchema();
$destination_table_schema->setFields($schema_fields);
// Set the load configuration, including source file(s) and schema
$load_configuration = new Google_JobConfigurationLoad();
$load_configuration->setSourceUris(array('gs://YOUR_GOOGLE_CLOUD_STORAGE_BUCKET/file.csv'));
$load_configuration->setDestinationTable($destination_table);
$load_configuration->setSchema($destination_table_schema);
$job_configuration = new Google_JobConfiguration();
$job_configuration->setLoad($load_configuration);
$load_job = new Google_Job();
$load_job->setKind('load');
$load_job->setConfiguration($job_configuration);
$jobs = $bigqueryService->jobs;
$response = $jobs->insert($project_number, $load_job);
echo '<pre>';
print_r($response);
echo '</pre>';
$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Authorize Access to the BigQuery API</a>";
}
?>
</div>
</div>
</body>
</html>

Get Drop Down Menu, PHP SQL HTML

I am a student and also working in my university. I am not studying computer science.
I mainly do support stuff, but now I should take care of our internship database.
Task: Create a drop down menu from a town which a student choses, then show all internships in this town.
I have an existing SQL database, can create a drop down menu from it, but have NO idea how I can use the selected value. A submit button would be fine, any solution which works is fine.
How can I put the chosen value from drop down menu to a PHP variable?
<?php
$dbanfrage = "SELECT DISTINCT Stadt FROM $tabelle WHERE Land = 'China' OR Land = 'Taiwan' OR Land = 'VR China' ORDER BY STADT ";
$result = mysql_db_query ($dbname, $dbanfrage, $dbverbindung);
if (!$result)
{
$message = 'Ungültige Abfrage: ' . mysql_error() . "\n";
$message .= 'Gesamte Abfrage: ' . $dbanfrage;
die($message);
}
echo "<select>";
echo "<option>Stadt auswählen</option>"; // printing the list box select command
while($nt = mysql_fetch_array($result)) //Array or records stored in $nt
{
echo "<option value = $nt[id]>$nt[Stadt]</option>"; /* Option values are added by looping through the array */
}
echo "</select>"; // Closing of list box
mysql_close ($dbverbindung);
?>
Just suggesting how you can do it --
<select name="country" id="country" onChange="showState();" >
<option value="">Select Country</option>
<option value="1">India</option>
<option value="2">Nepal</option>
</select>
Your Script--
<script type="text/javascript">
function showState( )
{
var value = document.getElementById('country').value;
var url = '<?php echo base_url ?>showstate.php';
$.ajax({
type: "GET",
url: url,
data:{'country':value},
success:function(results)
{
$('#div_state').html(results);
}
});
}
</script>
Your showstate.php php page --
//INCLUDE CONNECTION file to for db query
$type = $_GET['country'];
//Your DB QUERIES
This will load the content in your #div_state div. You can also use Jquery For this..i think this will help you :) an d let me know if you have any query.
<select name="name">
<option value="select">select</option>
<?php
$dbanfragee=mysql_query("SELECT DISTINCT Stadt FROM $tabelle WHERE Land = 'China' OR Land = 'Taiwan' OR Land = 'VR China' ORDER BY STADT ");
while($row=mysql_fetch_array($$dbanfrage)){?>
<option value="<?=$row['id']?>"><?=$row['stadt']?></option>
<?php }?>
</select>

Video upload with Graph API from user's desktop

i am trying to upload a local video to Facebook through the Graph API.
Here is an example from Facebook:
http://developers.facebook.com/blog/post/493/
That works nice, and i get an JSON response after submitting the from.
How can I get the respone ID, for use in, e.g. Javascript or directly as an GET-parameter on my server?
I don't want to upload the video on an public accessable server and then redirect the video upload to Facebook by using curl or something like that.
Has anyone an idea or an example of how i can get this to work?
Edit:
Here is the example i used:
<?php
$app_id = "YOUR_APP_ID";
$app_secret = "YOUR_APP_SECRET";
$my_url = "YOUR_POST_LOGIN_URL";
$video_title = "YOUR_VIDEO_TITLE";
$video_desc = "YOUR_VIDEO_DESCRIPTION";
$code = $_REQUEST["code"];
if(empty($code)) {
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&scope=publish_stream";
echo("<script>top.location.href='" . $dialog_url . "'</script>");
}
$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$access_token = file_get_contents($token_url);
$post_url = "https://graph-video.facebook.com/me/videos?"
. "title=" . $video_title. "&description=" . $video_desc
. "&". $access_token;
echo '<form enctype="multipart/form-data" action=" '.$post_url.' "
method="POST">';
echo 'Please choose a file:';
echo '<input name="file" type="file">';
echo '<input type="submit" value="Upload" />';
echo '</form>';
?>
Taken from this page: http://developers.facebook.com/blog/post/493/
This is the whole response i get from the form (JSON response):
{
"id": "xxxxx19208xxxxx"
}
The problem is that the JSON response is inline in the page.
I thought to set a target in the form to an inline iframe but then i will not be able to access the JSON code.

Best way to display current logged-on user in default.ctp?

I am in the process of customizing the default.ctp file and I am trying to display the currently logged on user's name on the top of the page.
In app_controller.php, I have the following:
function beforeFilter()
{
$user = $this->Auth->user();
if($user != null)
{
$this->Session->write('user_name',$user['User']['username']);
}
}
And in default.ctp, I have:
$user = $this->Session->read('Auth.User');
if(!empty($user))
{
echo 'Hello, ' . $user['user_name'];
}
However, it seems like the value $user_name is not getting set anywhere.
What am I doing wrong? Is there a better way to accomplish this?
Update: I've modified it as described in the answer, but it still doesn't work. I get an error:
Undefined index: user_name [APP/views/layouts/default.ctp, line 21]
you can also use the SessionHelper directly in the view / layout
$user = $this->Session->read('Auth.User');
if(!empty($user)) {
echo 'Hi ', $user['user_name'];
}
Cakephp 2.x:
<?php if (AuthComponent::user('id')): ?>
<p class="navbar-text pull-right">
Logged in as <?= AuthComponent::user('name') ?>
</p>
<?php endif; ?>
$user = $this->Session->read('Auth.User');
if(count($user))
echo $user['name'];