2017 © Pedro Peláez
 

sdk alchemyapi_php

A software development kit (sdk) for AlchemyAPI using PHP

image

alchemyai/alchemyapi_php

A software development kit (sdk) for AlchemyAPI using PHP

  • Wednesday, September 28, 2016
  • by alchemyai
  • Repository
  • 10 Watchers
  • 47 Stars
  • 956 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 57 Forks
  • 4 Open issues
  • 1 Versions
  • 6 % Grown

The README.md

alchemyapi_php

A software development kit (sdk) for AlchemyAPI using PHP - Please note that this legacy AlchemyAPI SDK is no longer supported by IBM. Please use the Watson SDKs https://github.com/watson-developer-cloud?utf8=✓&query=sdk, (*1)

AlchemyAPI

AlchemyAPI offers artificial intelligence as a service. We teach computers to learn how to read and see, and apply our technology to text analysis and image recognition through a cloud-based API. Our customers use AlchemyAPI to transform their unstructured content such as blog posts, news articles, social media posts and images into much more useful structured data., (*2)

AlchemyAPI is a tech startup located in downtown Denver, Colorado. As the world's most popular text analysis service, AlchemyAPI serves over 3.5 billion monthly API requests to over 35,000 developers. To enable our services, we use artificial intelligence, machine learning, neural networks, natural language processing and massive-scale web crawling. Our technology powers use cases in a variety of industry verticals, including social media monitoring, business intelligence, content recommendations, financial trading and targeted advertising., (*3)

More information at: http://www.alchemyapi.com, (*4)

API Key

To use AlchemyAPI, you'll need to obtain an API key and attach that key to all requests. If you do not already have a key, please visit: http://www.alchemyapi.com/api/register.html, (*5)

Demo

To use our interactive web demo, which includes access to all of AlchemyAPI's text analysis functions, please visit: http://www.alchemyapi.com/products/demo. You can use the demo to get a better understanding of how the text analysis functions work, and run some of your sample data through it to see what kind of results you can expect. The demo calls the exact same API functions as this SDK., (*6)

Getting Started with the PHP SDK

To get started and run the example, simply:, (*7)

git clone https://github.com/AlchemyAPI/alchemyapi_php.git
cd alchemyapi_php
php alchemyapi.php YOUR_API_KEY
php example.php

Just replace YOUR_API_KEY with your 40 character API key from AlchemyAPI, and you should be good to go., (*8)

Using the PHP SDK

This SDK makes accessing all of AlchemyAPI's text analysis functions with PHP easy. The following is a list of the available functions and how to use them within your application. For working code examples of these, please checkout the example code and run the example as described in the "Getting Started with the PHP SDK" section above., (*9)

Creating the AlchemyAPI object

To use each function, you must first create an AlchemyAPI object. The easiest way to do this is:, (*10)

<?php
    require_once 'alchemyapi.php';
    $alchemyapi = new AlchemyAPI();
?>

You can now use this object to access AlchemyAPI's text analysis functions., (*11)

Image Keywords

The Image Keywords API tags an image identified by a URL or image data posted in the body of an html request. For more high-level information on AlchemyAPI's Image keywords API, please visit: http://www.alchemyapi.com/products/features/image-tagging. For more technical information, please refer to the docs: http://www.alchemyapi.com/api/image-tagging., (*12)

To extract entities, use:, (*13)

<?php
    $response = $alchemyapi->image_keywords(FLAVOR, DATA, OPTIONS);
?>

Where FLAVOR can be 'url' or 'image' DATA is your url or uri-argument encoded image data, and OPTIONS is an array containing the optional parameters to modify the behavior of the call., (*14)

Options, (*15)

The following options are available for this call. To use, include your desired options into an array and pass it as the OPTIONS parameter in the call., (*16)

  • imagePostMode -> (only applicable to image flavor)
  • not-raw : pass an unencoded image file with "image=URI_ENCODED_DATA"
  • raw : pass an unencoded image file using POST
  • extractMode ->
  • always-infer : (more CPU intensive, more accurate)
  • trust-metadata : (less CPU intensive, less accurate) (default)
  • only-metadata : (even less CPU intensive, less accurate)

Parsing, (*17)

To parse the results, simply step through the response structure that is detailed in the docs. For example, here's how to print the keyword and score of each tagged image:, (*18)

<?php
    $response = $alchemyapi->image_keywords('url','http://www.lolcats.com/images/u/08/50/lolcatsdotcomur5dhkw464f8hb16.jpg', null);
    foreach ($response['imageKeywords'] as $imagekeyword) {
        echo 'keyword: ', $imagekeyword['text'], PHP_EOL;
        echo 'score: ', $imagekeyword['score'], PHP_EOL;
    }
?>

This should print out:, (*19)

keyword: cat
score: 0.999697
keyword: kitten
score: 0.942676

Entities

The entity extraction API identifies the proper nouns in your text, HTML or URL content. These are the named people, companies, organizations and locations. For more high-level information on AlchemyAPI's entity extraction API, please visit: http://www.alchemyapi.com/products/features/entity-extraction. For more technical information, please refer to the docs: http://www.alchemyapi.com/api/entity-extraction., (*20)

To extract entities, use:, (*21)

<?php
    $response = $alchemyapi->entities(FLAVOR, DATA, OPTIONS);
?>

Where FLAVOR can be 'text','html', or 'url', DATA is your text, html or url content, and OPTIONS is an array containing the optional parameters to modify the behavior of the call., (*22)

Options, (*23)

The following options are available for this call. To use, include your desired options into an array and pass it as the OPTIONS parameter in the call., (*24)

  • disambiguate -> disambiguate entities (i.e. Apple the company vs. apple the fruit). 0: disabled, 1: enabled (default)
  • linkedData -> include linked data on disambiguated entities. 0: disabled, 1: enabled (default)
  • coreference -> resolve coreferences (i.e. the pronouns that correspond to named entities). 0: disabled, 1: enabled (default)
  • quotations -> extract quotations by entities. 0: disabled (default), 1: enabled.
  • sentiment -> analyze sentiment for each entity. 0: disabled (default), 1: enabled. Requires 1 additional API transction if enabled.
  • showSourceText -> 0: disabled (default), 1: enabled
  • maxRetrieve -> the maximum number of entities to retrieve (default: 50)

Parsing, (*25)

To parse the results, simply step through the response structure that is detailed in the docs. For example, here's how to print the text and type of each extracted entity:, (*26)

<?php
    $response = $alchemyapi->entities('text','I like Ford cars better than icecream.', null);
    foreach ($response['entities'] as $entity) {
        echo 'entity: ', $entity['text'], PHP_EOL;
        echo 'type: ', $entity['type'], PHP_EOL;
    }
?>

This should print out:, (*27)

entity: Ford
type: Company

Keywords

The keyword extraction API identifies the important terms in your text, HTML or URL content. These are known by several names, but keywords, tags and terms are the most common. For more high-level information on AlchemyAPI's keyword extraction API, please visit: http://www.alchemyapi.com/products/features/keyword-extraction. For more technical information, please refer to the docs: http://www.alchemyapi.com/api/keyword-extraction., (*28)

To extract keywords, use:, (*29)

<?php
    $response = $alchemyapi->keywords(FLAVOR, DATA, OPTIONS);
?>

Where FLAVOR can be 'text','html', or 'url', DATA is your text, html or url content, and OPTIONS is an array containing the optional parameters to modify the behavior of the call., (*30)

Options, (*31)

The following options are available for this call. To use, include your desired options into an array and pass it as the OPTIONS parameter in the call., (*32)

  • keywordExtractMode -> normal (default), strict
  • sentiment -> analyze sentiment for each keyword. 0: disabled (default), 1: enabled. Requires 1 additional API transaction if enabled.
  • showSourceText -> 0: disabled (default), 1: enabled.
  • maxRetrieve -> the max number of keywords returned (default: 50)

Parsing, (*33)

To parse the results, simply step through the response structure that is detailed in the docs. For example, here's how to print the text and relevance of each extracted keyword:, (*34)

<?php
    $response = $alchemyapi->keywords('text', 'Bunnies are nice but sometimes robots are evil', array('sentiment'=>1));
    foreach ($response['keywords'] as $keyword) {
        echo 'keyword: ', $keyword['text'], PHP_EOL;
        echo 'relevance: ', $keyword['relevance'], PHP_EOL;
        echo 'sentiment: ', $keyword['sentiment']['type'], PHP_EOL;
    }
?>

This should print out:, (*35)

keyword: Bunnies
relevance: 0.981751
sentiment: positive
keyword: robots
relevance: 0.832378
sentiment: negative

Concepts

The concept tagging API identifies the high-level themes of your text, HTML or URL content. Unlike keywords, the concepts do not have to be specifically mentioned in the content. For more high-level information on AlchemyAPI's concept tagging API, please visit: http://www.alchemyapi.com/products/features/concept-tagging. For more technical information, please refer to the docs: http://www.alchemyapi.com/api/concept-tagging., (*36)

To tag concepts, use:, (*37)

<?php
    $response = $alchemyapi->concepts(FLAVOR, DATA, OPTIONS);
?>

Where FLAVOR can be 'text','html', or 'url', DATA is your text, html or url content, and OPTIONS is an array containing the optional parameters to modify the behavior of the call., (*38)

Options, (*39)

The following options are available for this call. To use, include your desired options into an array and pass it as the OPTIONS parameter in the call., (*40)

  • maxRetrieve -> the maximum number of concepts to retrieve (default: 8)
  • linkedData -> include linked data, 0: disabled, 1: enabled (default)
  • showSourceText -> 0:disabled (default), 1: enabled

Parsing, (*41)

To parse the results, simply step through the response structure that is detailed in the docs. For example, here's how to print the text and relevance of each concept:, (*42)

<?php
    $response = $alchemyapi->concepts('text','Today for lunch I ate two apples and one strawberry.', null);
    foreach ($response['concepts'] as $concept) {
        echo 'concept: ', $concept['text'], PHP_EOL;
        echo 'relevance: ', $concept['relevance'], PHP_EOL;
    }
?>

This should print out:, (*43)

concept: Fruit
relevance: 0.865419

Sentiment

The sentiment analysis API calculates the document-level sentiment of your text, HTML or URL content. Sentiment determines whether the content is positive or negative, and can be calculated for the entire document, a user-specified target, or at the keyword, entity or relational level. This API call is only for document sentiment. To get keyword, entity or relational sentiment, set the OPTION parameter {'sentiment':1} in the respective call. Targeted sentiment is handled by a seperate call as well. For more high-level information on AlchemyAPI's sentiment analysis API, please visit: http://www.alchemyapi.com/products/features/sentiment-analysis. For more technical information, please refer to the docs: http://www.alchemyapi.com/api/sentiment-analysis., (*44)

To calculate document-level sentiment, use:, (*45)

<?php
    $response = $alchemyapi->sentiment(FLAVOR, DATA, OPTIONS);
?>

Where FLAVOR can be 'text','html', or 'url', DATA is your text, html or url content, and OPTIONS is an array containing the optional parameters to modify the behavior of the call., (*46)

Options, (*47)

The following options are available for this call. To use, include your desired options into an array and pass it as the OPTIONS parameter in the call., (*48)

  • showSourceText -> 0: disabled (default), 1: enabled

Parsing, (*49)

To parse the results, simply step through the response structure that is detailed in the docs. For example, here's how to print the type and score of the document sentiment:, (*50)

<?php
    $response = $alchemyapi->sentiment('text','I really like chocolate cake.', null);
    echo 'sentiment: ', $response['docSentiment']['type'], PHP_EOL;
    echo 'score: ', $response['docSentiment']['score'], PHP_EOL;
?>

This should print out:, (*51)

sentiment: positive
score: 0.631595

NOTE: The sentiment type can be either positive, negative or neutral. The score is only returned for positive and negative sentiment, and is not included in the response structure for neutral sentiment., (*52)

Targeted Sentiment

The targeted sentiment analysis API calculates the document-level sentiment of your text, HTML or URL content in relation to a specified target. The targets are typically brand names, product names, specific features and similar. Please see the sentiment section above for more info about sentiment analysis., (*53)

To calculate targeted sentiment for an entire document, use:, (*54)

<?php
    $response = $alchemyapi->sentiment_targeted(FLAVOR, DATA, TARGET, OPTIONS);
?>

Where FLAVOR can be 'text','html', or 'url', DATA is your text, html or url content, TARGET is the user-specified target, and OPTIONS is an array containing the optional parameters to modify the behavior of the call., (*55)

Options, (*56)

The following options are available for this call. To use, include your desired options into an array and pass it as the OPTIONS parameter in the call., (*57)

  • showSourceText -> 0: disabled, 1: enabled

Parsing, (*58)

To parse the results, simply step through the response structure that is detailed in the docs. For example, here's how to print the type and score of the targeted sentiment:, (*59)

<?php
    $response = $alchemyapi->sentiment_targeted('text','My new phones screen is awesome, but the battery life is really bad.','battery life', null);
    echo 'sentiment: ', $response['docSentiment']['type'], PHP_EOL;
    echo 'relevance: ', $response['docSentiment']['score'], PHP_EOL;
?>

This should print out:, (*60)

sentiment: negative
relevance: -0.128125

NOTE: The sentiment type can be either positive, negative or neutral. The score is only returned for positive and negative sentiment, and is not included in the response structure for neutral sentiment., (*61)

Text

The text extraction API pulls out the important content from your HTML or URL content and leaves the unimportant content like navigation and ads behind. For more high-level information on AlchemyAPI's text extraction API, please visit: http://www.alchemyapi.com/products/features/text-extraction. For more technical information, please refer to the docs: http://www.alchemyapi.com/api/text-extraction., (*62)

To extract the text, use:, (*63)

<?php
    $response = $alchemyapi->text(FLAVOR, DATA, OPTIONS);
?>

Where FLAVOR can be 'html' or 'url', DATA is your html or url content and OPTIONS is an array containing the optional parameters to modify the behavior of the call., (*64)

Options, (*65)

The following options are available for this call. To use, include your desired options into an array and pass it as the OPTIONS parameter in the call., (*66)

  • useMetadata -> utilize meta description data, 0: disabled, 1: enabled (default)
  • extractLinks -> include links, 0: disabled (default), 1: enabled.

Parsing, (*67)

To parse the results, simply step through the response structure that is detailed in the docs. For example, here's how to print the extracted text:, (*68)

<?php
    $response = $alchemyapi->text('url','http://www.alchemyapi.com/products/features/text-extraction/',null);
    echo 'text: ', $response['text'], PHP_EOL;
?>

This should print out:, (*69)

text:  ... the text from the text extraction webpage ...

The above extracts the "cleaned" text. If you would like to get the raw text instead, use:, (*70)

<?php
    $response = $alchemyapi->text_raw(FLAVOR, DATA, OPTIONS);
?>

The parameters and parsing are the same as for the cleaned text call., (*71)

Author

The author extraction API identifies the author of your HTML or URL content. For more high-level information on AlchemyAPI's author extraction API, please visit: http://www.alchemyapi.com/products/features/author-extraction. For more technical information, please refer to the docs: http://www.alchemyapi.com/api/author-extraction., (*72)

To extract the author, use:, (*73)

<?php
    $response = $alchemyapi->author(FLAVOR, DATA, OPTIONS);
?>

Where FLAVOR can be 'html' or 'url', DATA is your html or url content, and OPTIONS is an array containing the optional parameters to modify the behavior of the call., (*74)

Options, (*75)

There are no available options for this call., (*76)

Parsing, (*77)

To parse the results, simply step through the response structure that is detailed in the docs. For example, here's how to print the author:, (*78)

<?php
    $response = $alchemyapi->author('url','http://www.grantland.com/story/_/id/9566213/bill-barnwell-puts-2013-detroit-vikings-numbers-test',null);

    echo 'author: ', $response['author'], PHP_EOL;
?>

This should print out:, (*79)

author: Bill Barnwell

Language

The language detection API identifies the language of your text, HTML or URL content. It currently supports detection of over 97 languages. For more high-level information on AlchemyAPI's language detection API, please visit: http://www.alchemyapi.com/products/features/language-detection. For more technical information, please refer to the docs: http://www.alchemyapi.com/api/language-detection., (*80)

To detect the language, use:, (*81)

<?php
    $response = $alchemyapi->language(FLAVOR, DATA, OPTIONS);
?>

Where FLAVOR can be 'text', 'html' or 'url', DATA is your text, html or url content, and OPTIONS is an array containing the optional parameters to modify the behavior of the call., (*82)

Options, (*83)

There are no available options for this call., (*84)

Parsing, (*85)

To parse the results, simply step through the response structure that is detailed in the docs. For example, here's how to print the language and ISO-639-1 code:, (*86)

<?php
    $response = $alchemyapi->language('text','donde estas el bano?', null);
    echo 'language: ', $response['language'], PHP_EOL;
    echo 'iso-639-1: ', $response['iso-639-1'], PHP_EOL;
?>

This should print out:, (*87)

language: spanish
iso-639-1: es

Title

The title extraction API identifies the title of your HTML or URL content. For more high-level information on AlchemyAPI's title extraction API, please visit: http://www.alchemyapi.com/products/features/text-extraction. For more technical information, please refer to the docs: http://www.alchemyapi.com/api/text-extraction., (*88)

To extract the title, use:, (*89)

<?php
    $response = $alchemyapi->title(FLAVOR, DATA, OPTIONS);
?>

Where FLAVOR can be 'html' or 'url', DATA is your html or url content, and OPTIONS is an array containing the optional parameters to modify the behavior of the call., (*90)

Options, (*91)

The following options are available for this call. To use, include your desired options into an array and pass it as the OPTIONS parameter in the call., (*92)

  • useMetadata -> utilize title info embedded in meta data, 0: disabled, 1: enabled (default)

Parsing, (*93)

To parse the results, simply step through the response structure that is detailed in the docs. For example, here's how to print the title:, (*94)

<?php
    $response = $alchemyapi->title('url','http://www.bloomberg.com/news/2013-08-13/florida-to-sue-georgia-in-u-s-supreme-court-over-water.html', null);
    echo 'title: ', $response['title'], PHP_EOL;
?>

This should print out:, (*95)

title: Florida to Sue Georgia in U.S. Supreme Court Over Water - Bloomberg

Relations

The relation extraction API identifies the Subject, Action, Object relations in your text, HTML or URL content. In addition to sentiment analysis, there are lots of options for this call, so be sure to check out the docs. For more high-level information on AlchemyAPI's relation extraction API, please visit: http://www.alchemyapi.com/products/features/relation-extraction. For more technical information, please refer to the docs: http://www.alchemyapi.com/api/relation-extraction., (*96)

To extract relations, use:, (*97)

<?php
    $response = $alchemyapi->relations(FLAVOR, DATA, OPTIONS);
?>

Where FLAVOR can be 'text','html', or 'url', DATA is your text, html or url content, and OPTIONS is an array containing the optional parameters to modify the behavior of the call., (*98)

Options, (*99)

The following options are available for this call. To use, include your desired options into an array and pass it as the OPTIONS parameter in the call., (*100)

  • sentiment -> 0: disabled (default), 1: enabled. Requires one additional API transaction if enabled.
  • keywords -> extract keywords from the subject and object. 0: disabled (default), 1: enabled. Requires one additional API transaction if enabled.
  • entities -> extract entities from the subject and object. 0: disabled (default), 1: enabled. Requires one additional API transaction if enabled.
  • requireEntities -> only extract relations that have entities. 0: disabled (default), 1: enabled.
  • sentimentExcludeEntities -> exclude full entity name in sentiment analysis. 0: disabled, 1: enabled (default)
  • disambiguate -> disambiguate entities (i.e. Apple the company vs. apple the fruit). 0: disabled, 1: enabled (default)
  • linkedData -> include linked data with disambiguated entities. 0: disabled, 1: enabled (default).
  • coreference -> resolve entity coreferences. 0: disabled, 1: enabled (default)
  • showSourceText -> 0: disabled (default), 1: enabled.
  • maxRetrieve -> the maximum number of relations to extract (default: 50, max: 100)

Parsing, (*101)

To parse the results, simply step through the response structure that is detailed in the docs. For example, here's how to print the subject, action and object of each extracted relation:, (*102)

<?php
    $response = $alchemyapi->relations('text','Bob broke my heart.',null);
    foreach ($response['relations'] as $relation) {
        if (array_key_exists('subject', $relation)) {
                echo 'Subject: ', $relation['subject']['text'], PHP_EOL;
        }

        if (array_key_exists('action', $relation)) {
                echo 'Action: ', $relation['action']['text'], PHP_EOL;
        }

        if (array_key_exists('object', $relation)) {
                echo 'Object: ', $relation['object']['text'], PHP_EOL;
        }
        echo PHP_EOL;
    }
?>

This should print out:, (*103)

Subject: Bob
Action: broke
Object: my heart

Category

The text categorization API identifies the high-level category of your text, HTML or URL content. The categories include values such as Arts & Entertainment, Business and Sports. For more high-level information on AlchemyAPI's text categorization API, please visit: http://www.alchemyapi.com/products/features/text-categorization. For more technical information, please refer to the docs: http://www.alchemyapi.com/api/text-categorization., (*104)

To detect the category, use:, (*105)

<?php
    $response = $alchemyapi->category(FLAVOR, DATA, OPTIONS);
?>

Where FLAVOR can be 'text', 'html' or 'url', DATA is your text, html or url content, and OPTIONS is an array containing the optional parameters to modify the behavior of the call., (*106)

Options, (*107)

The following options are available for this call. To use, include your desired options into an array and pass it as the OPTIONS parameter in the call., (*108)

  • showSourceText -> 0: disabled (default), 1: enabled

Parsing, (*109)

To parse the results, simply step through the response structure that is detailed in the docs. For example, here's how to print the category:, (*110)

<?php
    $response = $alchemyapi->category('url','http://www.washingtonpost.com/blogs/capital-weather-gang/wp/2013/08/14/d-c-area-forecast-ultra-nice-weather-dominates-next-few-days/', null);
    echo 'category: ', $response['category'], PHP_EOL;
?>

This should print out:, (*111)

category: weather

Feeds

The feed detection API extracts the RSS/ATOM feeds embedded in your HTML or URL content. For more high-level information on AlchemyAPI's feed detection API, please visit: http://www.alchemyapi.com/products/features/feed-detection. For more technical information, please refer to the docs: http://www.alchemyapi.com/api/feed-detection., (*112)

To detect the feeds, use:, (*113)

<?php
    $response = $alchemyapi->feeds(FLAVOR, DATA, OPTIONS);
?>

Where FLAVOR can be 'html' or 'url', DATA is your html or url content, and OPTIONS is an array containing the optional parameters to modify the behavior of the call., (*114)

Options, (*115)

There are no available options for this call., (*116)

Parsing, (*117)

To parse the results, simply step through the response structure that is detailed in the docs. For example, here's how to print the feeds:, (*118)

<?php
    $response = $alchemyapi->feeds('url','http://cnn.com', null);
    foreach ($response['feeds'] as $feed) {
        echo 'feed: ', $feed['feed'], PHP_EOL;
    }
?>

This should print out:, (*119)

feed: http://rss.cnn.com/rss/cnn_topstories.rss
feed: http://rss.cnn.com/rss/cnn_latest.rss

Microformats

The microformat parsing API extracts the microformats embedded in your HTML or URL content. For more high-level information on AlchemyAPI's microformats parsing API, please visit: http://www.alchemyapi.com/products/features/microformats-parsing. For more technical information, please refer to the docs: http://www.alchemyapi.com/api/microformats-parsing., (*120)

To detect the microformats, use:, (*121)

<?php
    $response = $alchemyapi->microformats(FLAVOR, DATA, OPTIONS);
?>

Where FLAVOR can be 'html' or 'url', DATA is your html or url content, and OPTIONS is an array containing the optional parameters to modify the behavior of the call., (*122)

Options, (*123)

There are no options available for this call., (*124)

Parsing, (*125)

To parse the results, simply step through the response structure that is detailed in the docs. For example, here's how to print the microformats:, (*126)

<?php
    $response = $alchemyapi->microformats('url','http://semanticweb.com/semanticweb-com-%E2%80%9Cinnovation-spotlight%E2%80%9D-interview-with-elliot-turner-ceo-of-alchemyapi_b30913', null);
    foreach ($response['microformats'] as $microformat) {
        echo 'field: ', $microformat['field'], PHP_EOL;
        echo 'data: ', $microformat['data'], PHP_EOL;
    }
?>

This should print out:, (*127)

field: RelTagLink
data: http://semanticweb.com/tag/alchemyapi
field: RelTag
data: alchemyapi

The Versions

28/09 2016

dev-master

9999999-dev http://www.alchemyapi.com/

A software development kit (sdk) for AlchemyAPI using PHP

  Sources   Download

Apache-2.0

php sdk ai alchemyapi alchemyai