Integrate API

Integrate API - Header

Friday, July 10, 2015

How to integrate Simple Captcha

Captcha is one of powerful technique to prevent automated data submission in a form. All developers must agree that, it's the best and useful technique where user data input need to allow. for example in a signup form or login or even user comments. Although for some visitors it's annoying but no alternative at all. So the best practice is to use a simple captcha. Most popular captcha is re-captcha which is extremely complicated and not user friendly at all. So here i am going to show you how you can integrate a very simple captcha in few minutes.

A simple and good one will be if we are able to create a captcha using php GD library. Almost all recent linux hosted servers have PHP GD2 library support, so we don't need to rethink about it.

in your signup/login/other form:

<?php
session_start();
include("simple-php-captcha.php");
if(isset($_POST['submit']))
{
  if($_POST['captcha_text']==$_SESSION['captcha']['code'])
    echo "Correct";
  else
    echo "Incorrect";
}
$_SESSION['captcha'] = simple_php_captcha();
?>
download simple-php-captcha.php file from here.

Here is your sample form:
<form action="" method="post" enctype="multipart/form-data">
 Username: <input type="text" name="username" /><br />
 Password: <input type="password" name="password" /><br />
 <img src="<?php echo  $_SESSION['captcha']['image_src'];?>" /><br />
 Captcha: <input type="text" name="captcha_text" />
 <input type="submit" value="Submit" name="submit" />
</form>

Make sure backgrounds and fonts folders are in same path to simple-php-captcha.php file.

Happy Captcha !!!

Tuesday, January 6, 2015

How to integrate Twilio.com API in php

You can integrate twilio call and SMS api with your application. Let me start with Twilio SMS API.

I am sure you are planning to send marketing/quiz/service offering or any other SMS to thousands of numbers. Before dive in to coding let me make clear couple of very important considerations for you. Two basic understanding is very important to understand twilio API.
Twilio Number: it's an US number you can purchase from twilio. you can use it as your sender id while sending SMS. Its also said long code because it's 10 digit long.
Alternatively, Short code means 5-6 digit long number which also available to purchase at twilio.

Keep in mind, You can send only 1(one) message per second per Twilio number. However, if you're sending same sms(usually marketing offers) to a list of numbers, they are said bulk SMS, which long code numbers are not designed for.

Carriers often block high volumes of SMS with same content/text sent out from one long code phone number(twilio number) as a precaution. In order to reduce the chance of happening, below are some useful suggestions you may follow.

- Vary/Change the content of your messages.
- Don't send all SMS in one shoot. instead sending it out over time. Using scheduler is a great idea undoubtedly.
- Use more Twilio/Long numbers.
- Try not to use marketing links or phrases.
- In worst case, replace non-working sending numbers or consider one/multiple short code  for more detail of short code click here.

All right!!! Say, i am owner of a laundry shop and i offer 15% discount for winter seasons. So how would i send this discount offer to my clients using twilio? The best points keep in mind:

  1. Using 1 twilio number send maximum 10 sms per minute.
now calculate how many sms you need and how much time you plan to spent for sending all SMS. i am sure you are good in divide calculation and purchase twilio number as much as you need. this is the best way i ever found. keep in mind everything depends on carriers filtering mechanisms which changes very frequently. So use as much twilio number as you affort.

Well, lets focus on how to send a SMS using twilio API. In order to do that, you need to find out two credentials SID and TOKEN. Log in twilio.com and from dashboard you will get both sid and token.
In short let me show you how easy to use twilio API for sending SMS. here is the php code.

<?php
require_once('../twilio-php/Services/Twilio.php'); // Loads the Twilio SMS API library
$twilio_sid="AC6xxx2e0dcbd341cff1xxxxfc73181xxx";
$twilio_token="exxxx34a4532xxxxaa0129d97xxxx0e0";
$sender_id="1234567890";
$recipient_no="9876543210";
$sms_text="Hello guys";
$client = new Services_Twilio($twilio_sid, $twilio_token);
$client->account->messages->sendMessage($sender_id, $recipient_no, $sms_text);
?>

Thats it. Enjoy!!!

Tips:
In order to check non-interested recipient please check incoming sms text. if it contain STOP, STOPALL, END, QUIT, CANCEL or UNSUBSCRIBE text then mark this recipient number as non-interested and never send SMS to this number. Twilio keeps track of these type of recipient against your virtual number(sender no)so that you can't bother them again. So, it's good practice to either delete those number from your database or mark them specially so that in future you never send them SMS from this number.