Keywords suggestion using AlchemyAPI

I was developing a web application for client which has a CMS (Content Management System) feature. Client will be able to manage website pages through this CMS module. One of the requirements was to ensure page SEO by meta keywords and meta description.

I was thinking a possible use case to accomplish SEO feature may be automatic generation of keywords based on page body. So I was searching for keyword generation API (Application Programming Interface). I found AlchemyAPI. Now I am going to implement this API.

I am using CakePHP (php framework), Bootstrap (css framework), jQuery (javascript framework). So here is my use case:

  • There will be a link of “create keywords” by meta-keywords form field
  • After clicking the link, an ajax request is being made to my CakePHP controller
  • In controller, a request will be sent to AlchemyAPI server and grab the response
  • If success, return the results by json
  • In CMS page, ajax response will be populated to the meta-keywords form field.
Now lets look at the page screenshots:
Sending ajax request
Received the response

Lets make the code:

Javascript code

function createKeywords(source, target){
 var target = target;
 var source = $('#'+source).val();
            type: "POST",
            url: '',
            data: {'words': source},
            dataType: "json",
            cache: false,
            success: function(msg){
                if(msg.msg == "Success"){
                    alertify.alert("An error occured. Try again."+msg.result);
        return false;

CakePHP Controller code

public function get_key_words(){
     if ($this->request->is('post') || $this->request->is('put')){
         App::import('Vendor', 'Alchemyapi', array('file' => 'alchemyapi' . DS . 'alchemyapi.php'));
            $words = trim($this->request->data['words']);
                $alchemyapi = new AlchemyAPI();
                $response = $alchemyapi->keywords('html', $words, array('sentiment'=>0));
                if($response && $response['status'] == 'OK'){
                    $key = Set::extract('/text', $response['keywords']);
                        $keywords = implode(', ', $key);
                        $msg = array(
                            'msg' => 'Success',
                            'result' => $keywords
                        $msg = array(
                            'msg' => 'Error',
                            'result' => 'No keywords'
                    $msg = array(
                        'msg' => 'Error',
                        'result' => 'Keyword Server Error'
                $msg = array(
                    'msg' => 'Error',
                    'result' => 'No Source text'
            Configure::write('debug', 0);
            echo json_encode($msg);


AlchemyAPI client

I used their php API client available here and github. Also read their documentation.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.