Home > Beginners, code, programming, seo > PHP AJAX: yahoo tag suggestion

PHP AJAX: yahoo tag suggestion

Note: code of the below post is taken from one of the very famous Wordpress plug-in Simple Tags.

during my first few post, i was so much confused about what and how i ll write.. and that time there was one more difficulty. how i ll give tags to my post which ll result in some traffic..

so then i got Simple Tag and that day i decided when ever i ll develop blog or anything similar i ll surely develop this feature in it, and i did it.

so now how does it work..

first write your article and then on some event we will send your article to yahoo API and in response it will return us tags. for this your server must support cURL those who don’t know what it is please click here. most of the time it is available on Linux servers and for if you are windows user and wanna know how to install in on WAMP or XAMPP please click here.

moving back to main topic so here is server side code.

$yahoo_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$yahoo_api_host = 'search.yahooapis.com'; // Api URL
$yahoo_api_path = '/ContentAnalysisService/V1/termExtraction'; // Api URL

$content = stripslashes($_REQUEST['text']) .' '. stripslashes($_REQUEST['title']);
$content = strip_tags(trim($content));

if (!empty($content) ) {

$tags = stripslashes($_REQUEST['tags']);

$param = 'appid='.$yahoo_id; // Yahoo ID
$param .= '&context='.urlencode($content); // Post content
if ( !empty($tags) ) {
$param .= '&query='.urlencode($tags); // Existing tags
}
$param .= '&output=php'; // Get PHP Array !

$data = '';

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, 'http://'.$yahoo_api_host.$yahoo_api_path.'?'.$param);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);

$data = curl_exec($curl);
curl_close($curl);
$data = unserialize($data);

$data = (array) $data['ResultSet']['Result'];
echo "##@@##";
foreach ( $data as $term ) {
echo ''.$term.'#-#';
}
echo "##@@##";
}

you might get confused in last few lines with # and @, basically its gonna be AJAX response, so to avoid everything else and just to get important content from response i cover up data with some unusal strings which are not commonly used.

and now its time for demo, demo???? yes now i have also decided to give demo if possible… :)

. please let me know if any modification required that ll be gr8 :) .

DEMO | DOWNLOAD

  • Stumbleupon
  • Delicious
Share it If you like it
  • Digg
  • Reddit
  • Facebook
  • StumbleUpon
  • Twitter
  • Yahoo! Buzz
  • Google Bookmarks
  • del.icio.us
  • DZone
  • email
  • BlinkList
  • LinkedIn
  • Live
  • RSS
  • Technorati

Related Posts