Not logged in - Login
< back

Library: PHP

We have made a library for PHP to help you start using A Trigger in a minute. This PHP library is made to avoid possible errors in your application. Even if ATrigger.com API server become unavailable, your application will work without any problem. On the other hand, all requests are Async by default and no slowness will happen.


Prerequisites

  1. First you need to follow Quick Start Guide.
  2. You have this personal details from your Account Setup page:
    1. API Key
    2. API Secret
  3. PHP Sockets enabled server


Step 1: Initialize

First you need to download the PHP Library:

Download PHP Library

Then, add the following to your PHP script:

require_once("/path/to/atriggerphp/ATrigger.php");
//require(dirname(__FILE__) . '/ATriggerPHP/ATrigger.php');

You only need to call init once when your php file is requested. All of your files will then have access to the same ATrigger client.

ATrigger::init("YOUR_APIKey","YOUR_APISecret");
//Debug-friendly: ATrigger::init("YOUR_APIKey","YOUR_APISecret", false, true);

In development you might want to use development settings above to start debugging.


Step 2: Start Using, Functions

Create

//Tags: Tags are required to identify tasks. 
//read more at: http://atrigger.com/docs/wiki/9/rest-api-v10-parameter-tag_
$tags = array();
$tags['type']='test';
            
//Create
ATrigger::doCreate("1month", "http://www.example.com/myTask?something", $tags);


  1. Required _timeSlice: Xminute, Xhour, Xday, Xmonth, Xyear - more info
  2. Required url: The target url that A Trigger will call it at defined TimeSlice. The library will automatically encode this url. Don't use Server.URLEncode()
  3. Required tags: You need tag your tasks for future identification and to control them using API in the future. read more at tags_* parameters.
  4. [Optional] retries: How many times should try if your server failed(or it was down)? default value: 3
  5. [Optional] count: How many cycles should be repeated? read more at count parameter.
  6. [Optional] first: When should be the first call? You are not required to set time value by default. read more at first parameter.
  7. [Optional] postData: The array for the content you want to HTTP POST to your URL when your task has been triggered.


More advanced example: This code will call your task URL at 1/1/2015. No repeats, no cycles. It will retries 5 times if your server be down.

$tags = array();
$tags['type']='test';

$postData = array();
$postData['type']='test';

$firstDate = date_create_from_format('d/M/Y:H:i:s', '01/Jan/2015:00:00:01');

ATrigger::doCreate("0minute", "http://www.example.com/myTask?something", $tags, $firstDate, 1, 5, $postData);


Delete

//Tags:
$tags = array();
$tags['type']='test';

//Delete
ATrigger::doDelete($tags);

Attention: This line will remove the task you've created in the last function(That is tagged "type=test").


  1. Required tags: All tasks with this tags will be removed. read more at tags_* parameters.


Pause

//Tags:
$tags = array();
$tags['type']='test';

//Pause
ATrigger::doPause($tags);


  1. Required tags: All tasks with this tags will be paused. read more at tags_* parameters.


Resume

//Tags:
$tags = array();
$tags['type']='test';

//Resume
ATrigger::doResume($tags);


  1. Required tags: All tasks with this tags will be resumed. read more at tags_* parameters.


Verify Request

All previous functions was for controlling tasks. but verifyRequest will be used to verify URL calls when you want to do a task to make sure the request is made from ATrigger servers, using the request IP and verifyRequest function:


//Get real IP of request
//http://stackoverflow.com/a/5785956/172163
function getRealIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}


//Check IP
if(ATrigger::verifyRequest(getRealIpAddr())) {
     //Invalid request: 
     //The request is not called from ATrigger servers.
} else {
     //Valid request:
     //Do the task here
}


  1. Required ip: The request IP that you want to validate if it is an IP of ATrigger.


More

How Pause and Resume works?