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.
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:
select * from weather.forecast where location=90210
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:
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;
}
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) :
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);
}
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:
function getFollowers($guid){
$yql = 'SELECT * FROM meme.followers WHERE owner_guid="'.$guid.'"';
$results = query($yql);
$results = xml2array($results);
return $results['query'];
}
Get a list of people this meme follows:
function getFollowing($guid){
$yql = 'SELECT * FROM meme.following WHERE owner_guid="'.$guid.'"';
$results = query($yql);
$results = xml2array($results);
return $results['query'];
}
Search in meme:
function searchMeme($query){
$yql = 'SELECT * FROM meme.memes WHERE query="'.$query.'"';
$results = query($yql);
$results = xml2array($results);
return $results['query'];
}
Get the Popular List:
function getPopular($locale){
$yql = 'SELECT * FROM meme.popular WHERE locale="'.$locale.'"';
$results = query($yql);
$results = xml2array($results);
return $results['query'];
}
Get the Timeline of a meme:
function getTimeline($guid){
$yql = 'SELECT * FROM meme.posts WHERE owner_guid="'.$guid.'"';
$results = query($yql);
$results = xml2array($results);
return $results['query'];
}
Get your Dashboard:
function getDashboard(){
$yql = 'SELECT * FROM meme.user.dashboard';
$results = query($yql);
$results = xml2array($results);
return $results['query'];
}
Follow a meme:
function follow($guid){
$yql = 'INSERT INTO meme.user.following (guid) VALUES ("'.$guid.'")';
$results = query($yql);
$results = xml2array($results);
return $results['query'];
}
You can use all of the above functions like this:
$followers = getFollowers('GUID HERE');
We would like to have some commentary on this one and maybe the development of a Wordpress plugin would be nice. Any volunteers?
Popularity: 2%
Related posts:
- meme from Yahoo!. My first impressions Today i got my meme invitation and i am finally...
- A Jeez.eu implementation of an URL Shortener Two days ago a friend of mine suggested to me...
- Web 3.0 Will Be All About Web Services. Learn The Basics The web has evolved in many ways. From the static...
- Using Bing’s API To Create A Custom Search Engine Microsoft’s search engine Bing, is a getting more popular each...
- Technologies That Will Change The Web Some years ago, the mp3 format was invented. It was...
About the Author:
Filed under: Social Media, Tutorials - Trackback Uri









You can also get the output from YQL as JSON – which is much easier to parse in and convert into PHP arrays. Try format=json instead of xml.
Jonathan
Hello
congratulations for this post.
I am preparing a post about this api using java
when I publish I tell you
if you want
Sure! Let me know about it :)
I get Fatal error: Call to a member function get() on a non-object at line $response = $app->client->get(
sprintf(”http://%s/v1/yql”,
QUERY_WS_HOSTNAME),
array(’q’ => $yql, ‘format’ => ‘xml’), 30);
May you help me. Best regards
Get the latest Yahoo SDK library and it should be fixed.
Sorry I have the last one. I tried again with your link without success. I can get a response doing $response = $app->query($yql); (for example list the followers) but this don’t work to update the status. Have you a zip of the complete code, have you tried to update a status? Thanks a lot for your quick reply.
Oh!!! You are trying to update your status!! You should first use oAuth to autenticate with Yahoo! and afterwards you have write access. For more information on how to do this you can always check the Yahoo YQL meme guide or wait some days till we publish our featured post about that.