• Home
  • My Work
  • Using AlchemyData News API with Codeigniter 3.x
https://torbjornzetterlund.com/wp-content/uploads/2015/05/AlchemyAPI-logo-960x600_c.png

Using AlchemyData News API with Codeigniter 3.x


Alchemy has recently launched an API that gives you access to news and blog content already enriched with Alchemy’s full suite of natural language processing services. The AlchemyData News provides businesses with the ability to query the world’s news like a database.

You can use the AlchemyData News API to:

  • Create highly targeted searches
  • Curate news and blog content from over 75,000 unique sources
  • Perform intelligent trend analysis with up to 60 days of historical coverage

In my article I will show you how I created a simple form to Query the AlchemyData News API.

To get started I needed a platform to build my example on, I choosed to use the Yggdrasil web application framework,Yggdrasil provides a modular extension with modules that are structured by the MVC methodology (model, view, controller) and a RESTful client that allows us to make RESTful API calls to externals API’s.

I create a controller and a view, in the view i created a simple form to query the AlchemyData News API, the view  looks like this.

Search_News-

My controller calls the view, clicking the search button on the view an ajax request is sent to the controller, in which the controller builds the url string to query the AlchemyData News API service. Below is the code of my controller.

php defined('BASEPATH') OR exit('No direct script access allowed');
/**
*
* @package   yggdrasil
* @author  torbjornzetterlund
// ------------------------------------------------------------------------
*/
class Newssearch extends Admin_Controller {

protected $ci;
public function __construct()
{
    parent::__construct();

// Load the rest client spark
    $this->lang->load('analysis');
}

public function index() {
    // Load template details
    $this->template
    ->title($this->module_details['name'])
    ->append_css('module::bootstrap-switch.min.css')
    ->append_js('module::bootstrap-switch.min.js');
    $this->template->build('newssearch');
}

//
// Create a new Alchemy project
//
public function get_alchemy_news() {

    // Load the library
    $this->load->library('rest');

    $config = array('server' => Settings::get('alchemy_url'));
    // Run some setup
    $this->rest->initialize($config);

    // Adjust the settings
    $param[] = 'apikey=' . Settings::get('alchemy_news_api');
    $param[] = 'outputMode=json'; // Return JSON
    $param[] = 'compact=1'; //compact mode
    $param[] = 'start=' .urlencode($_GET['start']); // Start Day
    $param[] = 'end=' .urlencode($_GET['end']); // End date
    $param[] = 'maxResults=' .urlencode($_GET['maxresults']); // How many items

    // check if any data exist in the title field
    if (!empty($_GET['title'])) {
       $param[] = 'enriched.url.title=' .urlencode($_GET['title']); // Return the source text
    }
    
    // check if any data existthe thetextstring field
    if (!empty($_GET['textstring'])) {
       $param[] = 'enriched.url.text=' .urlencode($_GET['textstring']); // Return the source text
    }

    $querystring = "";
    // build up the Alchemy query string
    $querystring = 'label_format_string=enriched.url.title,enriched.url.url';
    // check if descriptionshould be returned
    if (!empty($_GET['descriptionss'])) {
       $querystring .= ',enriched.url.text';
    }
    // check if authors nameshould be returned
    if (!empty($_GET['authorss'])) {
       $querystring .= ',enriched.url.author';
    }
    // check if imageshould be returned
    if (!empty($_GET['imagess'])) {
       $querystring .= ',enriched.url.image';
    }
    // check if keywordshould be returned
    if (!empty($_GET['keywordss'])) {
       $querystring .= ',enriched.url.keywords';
    }

    $param[] = $querystring;
    // Make a call to alchemyapi with query string
    $queryresults = $this->rest->get(Settings::get('alchemy_get_news'), implode('&', $param));

    // Debug
    // $this->rest->debug();
    // unpack the query response
    $queryresults = json_decode(json_encode($queryresults), true);

    // if response exist build html response
    if (!empty($queryresults)) {

//
// Build up the HTML code 
//
        if (!empty($result)) {
            echo $result; 
        }
        die();
    }
}

Views
If everything worked out correctly the query results will be display below the search form like this

Search_News

You can download the full source code from the Yggdrasil repo on Github. The modules name under the subdirectory app/modules/ is newssearch.

To use the Yggdrasil repo – you need to obtain a free AlchemyData News API key – the next step is to add API Key to Yggdrasil – you do that by selecting settings and adding the API key as show in this image.

Alchemy API Settings

I’d love to hear what you think. Please comment below to share your thoughts.

Menu