Yahoo! Query Language is an expressive SQL-like language that lets you query, filter, join, and update data across the Web. The new Yahoo service, meme, is a social network (tumblr like) that allows users to share photos, audio, text and video with each other. You can read a review for meme here.
Meme, got it's place in the YQL and we will show you here how to manage your meme account from your own site.
First of all you need to download this file. It is a php library that manages queries to the YQL with PHP. It also includes the oAuth library and a JSON parser. The next step is to create an API key to use with the library. We have to note that some features don't need to get an API key to use them.
[caption id="attachment_394" align="alignnone" width="600" caption="Getting an API from Yahoo"][/caption]
So lets explain what YQL is. YQL is an SQL like language. So the basic syntax is exactly the same as SQL. An example query is this:
[code lang="sql"]select * from weather.forecast where location=90210[/code]
The YQL is based on data tables. Meme is the last addition to the data tables provided by Yahoo.
Lets move to some code. We first need to write some code that will query the YQL service and your meme account in general.
The Query:
[code lang="php"]function query($yql) {
require_once "Yahoo.inc";
$app = new YahooApplication($clientKey,$sharedKey);
$response = $app->client->get(
sprintf("http://%s/v1/yql",
QUERY_WS_HOSTNAME),
array('q' => $yql, 'format' => 'xml'), 30);
if(is_null($response) || $response["code"] != 200) {
return NULL;
}
$resultSet = $response["responseBody"];
return $resultSet;
}
[/code]
The $clientKey and $sharedKey variables are the ones you got from the Yahoo API generator page. The function above will manage the queries to the YQL service. You can save these vars in a database table and call them whenever you need them.
We also see that we set the response to be xml. I like working with arrays than xml in PHP. So, we will create another function that will parse xml code to array (Credits for this function should be given to the author but i lost the referring URL to the author's site. If it is yours please contact with us to get the credit) :
[code lang="php"]function xml2array($contents, $get_attributes = 1, $priority = 'tag'){
$parser = xml_parser_create('');
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, trim($contents), $xml_values);
xml_parser_free($parser);
if (!$xml_values)
return; //Hmm...
$xml_array = array ();
$parents = array ();
$opened_tags = array ();
$arr = array ();
$current = & $xml_array;
$repeated_tag_index = array ();
foreach ($xml_values as $data)
{
unset ($attributes, $value);
extract($data);
$result = array ();
$attributes_data = array ();
if (isset ($value))
{
if ($priority == 'tag')
$result = $value;
else
$result['value'] = $value;
}
if (isset ($attributes) and $get_attributes)
{
foreach ($attributes as $attr => $val)
{
if ($priority == 'tag')
$attributes_data[$attr] = $val;
else
$result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
}
}
if ($type == "open")
{
$parent[$level -1] = & $current;
if (!is_array($current) or (!in_array($tag, array_keys($current))))
{
$current[$tag] = $result;
if ($attributes_data)
$current[$tag . '_attr'] = $attributes_data;
$repeated_tag_index[$tag . '_' . $level] = 1;
$current = & $current[$tag];
}
else
{
if (isset ($current[$tag][0]))
{
$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
$repeated_tag_index[$tag . '_' . $level]++;
}
else
{
$current[$tag] = array (
$current[$tag],
$result
);
$repeated_tag_index[$tag . '_' . $level] = 2;
if (isset ($current[$tag . '_attr']))
{
$current[$tag]['0_attr'] = $current[$tag . '_attr'];
unset ($current[$tag . '_attr']);
}
}
$last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;
$current = & $current[$tag][$last_item_index];
}
}
elseif ($type == "complete")
{
if (!isset ($current[$tag]))
{
$current[$tag] = $result;
$repeated_tag_index[$tag . '_' . $level] = 1;
if ($priority == 'tag' and $attributes_data)
$current[$tag . '_attr'] = $attributes_data;
}
else
{
if (isset ($current[$tag][0]) and is_array($current[$tag]))
{
$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
if ($priority == 'tag' and $get_attributes and $attributes_data)
{
$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
}
$repeated_tag_index[$tag . '_' . $level]++;
}
else
{
$current[$tag] = array (
$current[$tag],
$result
);
$repeated_tag_index[$tag . '_' . $level] = 1;
if ($priority == 'tag' and $get_attributes)
{
if (isset ($current[$tag . '_attr']))
{
$current[$tag]['0_attr'] = $current[$tag . '_attr'];
unset ($current[$tag . '_attr']);
}
if ($attributes_data)
{
$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
}
}
$repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken
}
}
}
elseif ($type == 'close')
{
$current = & $parent[$level -1];
}
}
return ($xml_array);
}[/code]
Now all we have to do is query the YQL service.
Notes:
The $guid variable is an alphanumeric 26 chars long hash. For example Jeez Tech's guid is : 3FLGPU5NXTTGNZYK76WX2DD5SQ
The $locale variable is used to set the country locale of which you need data.
Get the followers of a meme:
[code lang="php"]function getFollowers($guid){
$yql = 'SELECT * FROM meme.followers WHERE owner_guid="'.$guid.'"';
$results = query($yql);
$results = xml2array($results);
return $results['query'];
}[/code]
Get a list of people this meme follows:
[code lang="php"]function getFollowing($guid){
$yql = 'SELECT * FROM meme.following WHERE owner_guid="'.$guid.'"';
$results = query($yql);
$results = xml2array($results);
return $results['query'];
}[/code]
Search in meme:
[code lang="php"]function searchMeme($query){
$yql = 'SELECT * FROM meme.memes WHERE query="'.$query.'"';
$results = query($yql);
$results = xml2array($results);
return $results['query'];
}[/code]
Get the Popular List:
[code lang="php"]function getPopular($locale){
$yql = 'SELECT * FROM meme.popular WHERE locale="'.$locale.'"';
$results = query($yql);
$results = xml2array($results);
return $results['query'];
}[/code]
Get the Timeline of a meme:
[code lang="php"]function getTimeline($guid){
$yql = 'SELECT * FROM meme.posts WHERE owner_guid="'.$guid.'"';
$results = query($yql);
$results = xml2array($results);
return $results['query'];
}[/code]
Get your Dashboard:
[code lang="php"]function getDashboard(){
$yql = 'SELECT * FROM meme.user.dashboard';
$results = query($yql);
$results = xml2array($results);
return $results['query'];
}[/code]
Follow a meme:
[code lang="php"]function follow($guid){
$yql = 'INSERT INTO meme.user.following (guid) VALUES ("'.$guid.'")';
$results = query($yql);
$results = xml2array($results);
return $results['query'];
}[/code]
You can use all of the above functions like this:
[code lang="php"]$followers = getFollowers('GUID HERE');[/code]
We would like to have some commentary on this one and maybe the development of a Wordpress plugin would be nice. Any volunteers?
↧