Product

Getting Started with the AYLIEN SDK for PHP

If you’re new to AYLIEN and don’t yet have an account you can take a look at our blog on getting started with the API or alternatively you can go directly to the getting started page on our website which will take you through the signup process. We provide a free plan which allows you to make up to 1,000 calls per day to the API for free forever.

Downloading and Installing the PHP SDK

All of our SDK repositories are hosted on Github. To use the SDK start by making the following addition to your composer.json.


{
  "require": {
    "aylien/textapi": "0.1.*"
  }
}

Once you’ve installed the SDK you’re ready to start coding! For the remainder of this blog we’ll walk you through making calls using the PHP SDK and show the output you should receive in each case while showcasing a few simple features of the API.

Configuring the SDK with your AYLIEN credentials

Once you’ve subscribed to our API and have downloaded the SDK you can start making calls by adding the following PHP code.


require __DIR__ . "/vendor/autoload.php";
$textapi = new AYLIENTextAPI("YourApplicationId", "YourApplicationKey");

When calling the API you can pass a piece of text directly to the API for analysis or you can pass a URL and we will automatically extract the main piece of text on that webpage.

Language Detection

Let’s take a look at the Language Detection endoint. The Language detection endpoint is uite straightforward. It will automatically tell you what language a piece of text is written in. In this example we’ll detect the language of the following sentence; ‘What language is this sentence written in?’

You can call the endpoint using the following piece of code.


$text = "What language is this sentence written in?";
$language = $textapi->Language(array("text" => $text));
echo sprintf("Text: %s <br/>", $language->text);
echo sprintf("Language: %s <br/>", $language->lang);
echo sprintf("Confidence: %F <br/>", $language->confidence);

You should receive an output similar to the one shown below which shows that the language detected was English and the confidence that it was detected correctly (a number between 0 and 1) is very close to 1 indicating that you can be pretty sure it is correct.

Language Detection Results


Text: What language is this sentence written in?
Language: en
Confidence: 0.999997

Sentiment Analysis

Next, we’ll look at analyzing the sentence “John is a very good football player” to determine it’s sentiment i.e. positive , neutral or negative. The endpoint will also determine if the text is subjective or objective. You can call the endpoint with the following piece of code


$text = "John is a very good football player!";
$sentiment = $textapi->Sentiment(array("text" => $text));
echo sprintf(" <br/>Text: %s <br/>", $sentiment->text);
echo sprintf("Sentiment Polarity: %s <br/>", $sentiment->polarity);
echo sprintf("Polarity Confidence: %F <br/>", $sentiment->polarity_confidence);

You should receive an output similar to the one shown below which indicates that the sentence is objective and is positive, both with a high degree of confidence.

Sentiment Analysis Results


Text: John is a very good football player!
Sentiment Polarity: positive
Polarity Confidence: 0.999999

Article Classification

Now we’re going to take a look at our Classification endpoint. The Classification endpoint automatically assigns an article or piece of text to one or more categories making it easier to manage and sort. The classification is based on IPTC International Subject News Codes and can identify up to 500 categories. The code below analyses a BBC news article about how animals eveolved on earth.


$url = "http://www.bbc.com/earth/story/20150112-did-snowball-earth-make-animals";
echo sprintf("<br/>Classification:<br/>");
$classify = $textapi->Classify(array("url" => $url));
echo sprintf("URL: %s", $url);
foreach($classify->categories as $val) {
  echo sprintf("<br/>Label        :   %s     ", $val->label);
  echo sprintf("<br/>IPTC code    :   %s     ", $val->code);
  echo sprintf("<br/>Confidence   :   %F     ", $val->confidence);
  }

When you run this code you should receive an output similar to that shown below which assigns the article an IPTC label of “natural science – geology” with an IPTC code of 13004001.

Article Classification Results


Classification:
URL: http://www.bbc.com/earth/story/20150112-did-snowball-earth-make-animals
Label : natural science - geology
IPTC code : 13004001
Confidence : 1.000000

Hashtag Analysis

Next, we’ll look at analyzing the same BBC article to extract hashtag suggestions for sharing the article on social media with the following code.


echo sprintf("<br/><br/>Hashtags:<br/>");
echo sprintf("URL: %s", $url);
$hashtags = $textapi->Hashtags(array("url" => $url));
foreach($hashtags->hashtags as $val) {
  echo sprintf(" <br/> %s", $val );
}

You should receive the output shown below.

Hashtag Suggestion Results


Hashtags:
URL: http://www.bbc.com/earth/story/20150112-did-snowball-earth-make-animals
#SnowballEarth
#Oxygen
#Earth
#Evolution
#CarbonDioxide
#Glacier
#GlacialPeriod
#OperationDeepFreeze
#CambrianExplosion ....

If your more of a node of Java fan, check out the rest of our SDKs for node.js, Go, Ruby, Python, Java and .Net (C#). For more information regarding the APIs go to the documentation section of our website.

We’re publishing ‘getting started’ blogs for the remaining SDKs over the coming weeks so keep an eye out for them.





Text Analysis API - Sign up




Author


Avatar

Mike Waldron

Head of Marketing & Sales @ AYLIEN A legal convert with a masters degree from Smurfit Business School, Mike runs our Sales and Marketing at AYLIEN. Mike gathered his Sales and Marketing experience with technology companies in Sydney and Dublin before getting the startup itch and joining the team at AYLIEN. Twitter: @MikeWallly