Using social networking APIs to prevent spam + code samples

In a recent blog post I wrote briefly about some ideas relating to using popular APIs to help identify and prevent spam that can happen with forms on a website.

I wanted to follow up on this with some further thoughts and some code examples for anyone that might like to use them.

Here is an example of using the Rapleaf utility API with PHP to determine the gender of a name.  This could be handy if you are unsure of whether to write ‘Dear Sir’ or ‘Madam’ in a reply if you are inclined to reply so formally!

The following example sends a name to the name_to_gender service and receives the JSON response in to a PHP array called $response_array.

$name = urlencode($name);
$response = file_get_contents(“http://api.rapleaf.com/v4/util/name_to_gender/$name”);
$response_array = json_decode($response, TRUE);

Output:

Array ( [status] => OK [answer] => Array ( [input] => Bill [gender] => Male [likelihood] => 0.992195 ) )

Here is an example of using the Rapleaf API to gather more information about an email address:

$response = file_get_contents(“https://personalize.rlcdn.com/v4/dr?email=XXXXX&api_key=YYYYY&show_available”);
$response_array = json_decode($response, TRUE);
return $response_array;

In this sample, you will need to replace XXXXX with your email address and YYYYY with your own API Key which you can sign up to for free.

Output:

Array ( [location] => Cork, County Cork, Ireland [gender] => Male [influencer_score] => 71-80 )

Here is a PHP example of connecting to the Lymbix toneAPI using CURL to determine the sentiment of a piece of content:

$url = “http://gyrus.lymbix.com/tonalize”;
$header_information = array(‘AUTHENTICATION: XXXXXXXXXXX’,
‘ACCEPT: application/json’,
‘VERSION: 2.1’);

$data_information = array(
‘article’ => $message,
‘return_fields’ => ‘[]’);

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_information);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_information);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$result = json_decode(curl_exec($ch), TRUE);
curl_close($ch);
return $result;

In this example, I am sending the content $message to the Lymbix API located at http://gyrus.lymbix.com/tonalize. XXXXXXX would need to be replaced with your own API Key.

A sample output from the ToneAPI for the quote:

“Determine never to be idle. No person will have occasion to complain of the want of time who never loses any. It is wonderful how much may be done if we are always doing.” – Thomas Jefferson

Output:

Array ([article] => Determine never to be idle. No person will have occasion to complain of the want of time who never loses any. It is wonderful how much may be done if we are always doing. – Thomas Jefferson
[ignored_terms] => Array ( [0] => No [1] => Thomas )
[affection_friendliness] => 0.45
[enjoyment_elation] => 1.27
[amusement_excitement] => 0.82
[contentment_gratitude] => 0.43
[sadness_grief] => -2.85
[anger_loathing] => -5.82
[fear_uneasiness] => -0.74
[humiliation_shame] => -0.71
[dominant_emotion] => anger_loathing
[average_intensity] => 0.92
[article_sentiment] => Array ( [sentiment] => Negative [score] => -0.57 )
[coverage] => 32
[intense_sentence] => Array ( [sentence] => No person will have occasion to complain of the want of time who never loses any.
[dominant_emotion] => anger_loathing
[intensity] => 0.2 )
[clarity] => 61.65 )

Its quite a detailed response showing the different sentiments it can pick up in the text and how much of the text it understood.

Adding additional information to incoming emails

While working on this I thought it could be great to add information to my regular emails, not just those that come in via forms on a website.

There are already some great tools for a users email inbox, such as Xobni which provides a profile with additional information about the person you are communicating with.  I use Mozilla Thunderbird for my emails so unfortunately this add-on isn’t available to me.

I connect to my email server using IMAP so I wondered if I could connect to my mail server and using some of the APIs, append some additional information to incoming emails directly in to the email content rather than using an add-on to the email client.

Using PHP, I was able to connect to my IMAP inbox and determine the email address of the sender. Using Rapleaf, I looked up the email address and added the results to the body of the incoming email. Its handy if I receive an email from someone I have never communicated with before.

If an API could be developed to add additional information to a phone number and display it on the screen of a phone during an incoming call, now that that would be cool!

Can social APIs help prevent fraud?

Recently, I was working on a client’s website, updating some older code. Their site made great use of a set of APIs combined to provide a service to their customers. It included an online payment gateway to charge the customers and their system works quite well.

My work involved replacing a deprecated API.  This changed the overall process of the system quite significantly and I spent some time updating the logging and notifications the owners used to monitor the system.

Their site receives an amount of traffic which tries to game or take advantage of their system to try to avail of their service without paying, so it’s important to have plenty of logs and techniques to monitor and protect the system.

In a situation like this it could make a lot of sense to also incorporate an API which might add additional information about the user making a transaction. Perhaps payment gateway providers such as Paypal or Worldpay already do this behind the scenes to help prevent fraud?

In a case where the identity of a buyer is in question, a system adding additional information such as age, gender and location could help a lot in deciding whether to block the transaction or allow it to proceed.

Leave a Reply

Your email address will not be published. Required fields are marked *