POSTing JSON Data With PHP cURL

$data = array("name" => "Hagrid", "age" => "36");                                                                    
$data_string = json_encode($data);                                                                                   

$ch = curl_init('http://api.local/rest/users');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   

$result = curl_exec($ch);

Background – An Overview of the Crontab

Let’s face it, having the ability to schedule tasks to run in the background is just great! From backing up an SQL database, fetching / sending emails to running clean up tasks, analyzing performance, or even grabbing RSS feeds, cron jobs are fantastic!

Although the syntax of scheduling a new job may seem daunting at first glance, it’s actually relatively simple to understand once you break it down. A cron job will always have five columns each of which represent a chronological ‘operator’ followed by the full path and command to execute:

  1. * * * * * home/path/to/command/the_command.sh

Each of the chronological columns has a specific relevance to the schedule of the task. They are as follows:

  • Minutes represents the minutes of a given hour, 0-59 respectively.
  • Hours represents the hours of a given day, 0-23 respectively.
  • Days represents the days of a given month, 1-31 respectively.
  • Months represents the months of a given year, 1-12 respectively.
  • Day of the Week represents the day of the week, Sunday through Saturday, numerically, as 0-6 respectively.
  1. Minutes [0-59]
  2. |   Hours [0-23]
  3. |   |   Days [1-31]
  4. |   |   |   Months [1-12]
  5. |   |   |   |   Days of the Week [Numeric, 0-6]
  6. |   |   |   |   |
  7. *   *   *   *   * home/path/to/command/the_command.sh

So, for example, if one wanted to schedule a task for 12am on the first day of every month it would look something like this:

  1. 0 0 1 * * home/path/to/command/the_command.sh

If we wanted to schedule a task to run every Saturday at 8:30am we’d write it as follows:

  1. 30 8 * * 6 home/path/to/command/the_command.sh

There are also a number of operators which can be used to customize the schedule even further:

  • Commas is used to create a comma separated list of values for any of the cron columns.
  • Dashes is used to specify a range of values.
  • Asterisksis used to specify ‘all’ or ‘every’ value.

Retrieve Google Apps Users

$options = array(
‘requestScheme’ => Zend_Oauth::REQUEST_SCHEME_HEADER,
‘version’ => ‘1.0’,
‘signatureMethod’ => ‘HMAC-SHA1’,
‘consumerKey’ => $consumer_Key,
‘consumerSecret’ => $consumer_secret_key
);
/**
* Create HTTP Client object with OAuth
*/
try {
$oauthConsumer = new Zend_Oauth_Consumer($options);
$token = new Zend_Oauth_Token_Access();
$httpClient = $token->getHttpClient($options);
$userClient = new Zend_Gdata_Gapps($httpClient,$domain);
$users = $userClient->retrieveAllUsers();
}catch(Exception $e){
echo $e->getMessage();
}