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();
 tinyMCE.triggerSave(true);
        $.ajax({
            type: "POST",
            url: 'http://website.com/cms/get_key_words',
            data: {'words': source},
            dataType: "json",
            cache: false,
            success: function(msg){
                //alert(msg);
                //console.log(msg);
                if(msg.msg == "Success"){
                    $("#"+target).val(msg.result);
                }else{
                    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']);
            
            if(!empty($words)){
                $alchemyapi = new AlchemyAPI();
                $response = $alchemyapi->keywords('html', $words, array('sentiment'=>0));
                //debug($response);exit;
                if($response && $response['status'] == 'OK'){
                    $key = Set::extract('/text', $response['keywords']);
                    if($key){
                        $keywords = implode(', ', $key);
                        $msg = array(
                            'msg' => 'Success',
                            'result' => $keywords
                        );
                    }else{
                        $msg = array(
                            'msg' => 'Error',
                            'result' => 'No keywords'
                        );
                    }
                    
                }else{
                    $msg = array(
                        'msg' => 'Error',
                        'result' => 'Keyword Server Error'
                    );
                }
            }else{
                $msg = array(
                    'msg' => 'Error',
                    'result' => 'No Source text'
                );
            }
            
            Configure::write('debug', 0);
            echo json_encode($msg);
            exit;
     }
 }

 

AlchemyAPI client

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

Comments

Leave a Reply