Skip to content

Instantly share code, notes, and snippets.

@caseysoftware
Last active February 1, 2016 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caseysoftware/557fac23cf3829a16fc4 to your computer and use it in GitHub Desktop.
Save caseysoftware/557fac23cf3829a16fc4 to your computer and use it in GitHub Desktop.
In this one, we use PHP to tie together the AlchemyData News API along with the Clarify.io API. This is part of a post for the IBM Watson blog: http://blog.alchemyapi.com/decoding-the-debates-a-cognitive-approach
<?php
// Don't forget to rename credentials-dist.php to credentials.php and insert your API key
require __DIR__ . '/credentials.php';
require __DIR__.'/../vendor/autoload.php';
use GuzzleHttp\Client as GuzzleClient;
$bundle_id = 'bundles/abcde12345';
$bundle = new Clarify\Bundle($apikey);
$insights = $bundle->insights;
$_data = $insights->load($bundle_id);
$insights->get($_data['_links']['insight:spoken_keywords']['href']);
$keywords = $insights->keywords;
$alchemy_client = new GuzzleClient(['base_uri' => 'https://gateway-a.watsonplatform.net/calls/data/GetNews']);
$options = [];
$options['apikey'] = 'alchemy-key';
$options['outputMode'] = 'json';
$options['start'] = 'now-5d';
$options['end'] = 'now';
$options['return'] = 'enriched.url.title,enriched.url.url';
$options['count'] = 10;
foreach($keywords as $keyword) {
echo $keyword['term'] . "\n";
$options['q.enriched.url.enrichedTitle.relations.relation'] = '|subject.text=' . $keyword['term'] . ',object.entities.entity.type=Person|';
$response = $alchemy_client->request('GET', '', $options);
$results = json_decode($response->getBody(), true);
$articles = $results['result']['docs'];
foreach($articles as $article) {
echo $article['source']['enriched']['url']['url'] . "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment