2 min read

Keywords suggestion using AlchemyAPI

I was developing a web application for a client which has a CMS (Content Management System) feature. The 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 the SEO features may be the automatic generation of keywords based on the 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 to “create keywords” by meta-keywords from field
  • After clicking the link, an ajax request is made to my CakePHP controller
  • In the controller, a request will be sent to the AlchemyAPI server and grab the response
  • If successful, return the results by JSON
  • In the CMS page, the ajax response will be populated to the meta-keywords form field.

Now let's look at the page screenshots:

Sending ajax request
Receiving the response

JS 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;
     }
 }

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