wordpress flash API – sample code – Part 1
so after some expermintations, I could finally get around to posting my findings.. available for people to grab should they wish to.
a big credit is due to Tim Wilson on his library which was the basis of all the work I have done in here. I only changed the return types of the functions to be of custom data types instead of XML.
steps of reproduction:
1- download the AMFPHP package from http://sourceforge.net/project/showfiles.php?group_id=72483#files
2- unzip the package in your flash export folder (WWW for example, or DEPLOY, or whatever you call it).. put all the AMF files inside a folder and name it as you wish (I am going to call it connectors for example
3- in services/DatabaseRequest.php fill in your appropriate username/password/db name and hostname
4-go to services/functions.php and write your functions there.. I am using these functions:
function getPostList()
{
$dbc = new DatabaseRequest();
$db_query = $dbc->call("SELECT * FROM wp_posts WHERE post_status='publish' AND post_type !='page' ORDER BY ID DESC");
$posts = array();
$i =0;
while ($fields = mysql_fetch_array($db_query))
{
$posts[$i] = array(id=>$fields['ID'] , title => $fields['post_title']);
$i ++;
}
return $posts;
}
and
function getCategoryList($parentCategorySlug)
{
$dbc = new DatabaseRequest();
$query = "SELECT wp_terms.slug, wp_terms.name, wp_term_taxonomy.description FROM wp_terms, wp_term_taxonomy WHERE wp_terms.term_id = wp_term_taxonomy.term_id AND wp_term_taxonomy.taxonomy = 'category'".
(($parentCategorySlug != "") ? "AND wp_term_taxonomy.parent IN ( SELECT term_id FROM wp_terms WHERE wp_terms.slug LIKE '".$parentCategorySlug."')" : "") ;
$db_query = $dbc->call($query);
$arr = array();
$i=0;
while ($fields = mysql_fetch_array($db_query))
{
$arr[$i] = array(title =>$fields['name'] , id => $fields['slug'] , description=>$fields['description']);
$i ++;
}
return $arr;
}
and
function getCategoryPosts($categorySlug)
{
$dbc = new DatabaseRequest();
$db_query = $dbc->call("SELECT * FROM wp_posts
LEFT JOIN wp_term_relationships ON ( wp_posts.ID = wp_term_relationships.object_id )
LEFT JOIN wp_term_taxonomy ON ( wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id )
WHERE wp_posts.post_status = 'publish' AND wp_term_taxonomy.taxonomy = 'category' AND wp_term_taxonomy.term_id in
(SELECT term_id FROM wp_terms WHERE wp_terms.slug LIKE '".$categorySlug."')" );
$posts = array();
$i =0;
while ($fields = mysql_fetch_array($db_query))
{
$indexQuery = "SELECT meta_value FROM wp_postmeta WHERE post_id =".$fields['ID']." AND meta_key LIKE 'index'";
$db_query1 = $dbc->call($indexQuery );
$index = mysql_fetch_array($db_query1);
$posts[$i] = array(index => $index[0] , id => $fields['ID'] , title =>$fields['post_title']);
$i++;
}
return $posts;
}
and
function getPost($postID)
{
$dbc = new DatabaseRequest();
$query ="
SELECT * FROM wp_posts WHERE wp_posts.ID=".$postID." AND wp_posts.post_status = 'publish'";
$db_query = $dbc->call($query);
$fields = mysql_fetch_array($db_query);
$post = $fields[0];
$arr=array();
if(count($fields) == 1)
{
createFailMessage('Post Not Found');
}
else
{
$post = array( id=> $fields['ID'] ,
title=>$fields['post_title'] ,
content => $fields['post_content'] ,
date=> $fields['post_modified_gmt']
);
// Get any Custom Fields for this Post.
$additionalFields = $dbc->call("SELECT * FROM wp_postmeta WHERE post_id='".$fields['ID']."';");
$extrFields = array();
$j=0;
while ($customField = mysql_fetch_array($additionalFields))
{
$extrFields[$j] = array(
index => $customField['meta_key'],
value => $customField['meta_value']
);
$j++;
}
$result = array(post => $post , meta =>$extrFields);
return $result;
}
the beauty of AMF/PHP is that you can get returns in the form of objects, arrays, multidimensional arrays, and objects that has properties that are a mix of arrays and regular data types..
so now, we have all the PHP functions that need to be called in order to get data from the Database… notice that in the getPost function, I am also returning the meta fields associated with the post, based on that post ID.
the flash part of that communication will be in part 2 of this post
[...] post is part two of the AMF/PHP connector class that I started in a previous thread talking about the PHP [...]