Difference between revisions of "PHP"

From MyMobileWorkers API
Jump to: navigation, search
(→‎Job.php)
(→‎Send.php)
Line 126: Line 126:
  
 
<source lang="php">
 
<source lang="php">
−
include('Job.php');
+
<?php
  
 +
include('job.php');
 +
//TODO include('config.php');
  
−
/* This is the file you execute */
 
−
 
−
 
−
//settings
 
−
$url = "http://live.mymobileworkers.com:8080/api/";
 
−
$apiKey = "ENTER HERE";
 
  
 
function sendJob($json,$method){
 
function sendJob($json,$method){
 
// Initializing curl
 
// Initializing curl
 +
 +
        $url = config::$url;
 +
        $apiKey = config::$apiKey;
 
$ch = curl_init( $url . $method );
 
$ch = curl_init( $url . $method );
 
$options = array(
 
$options = array(
−
+
 
CURLOPT_HTTPHEADER => array('Content-Type: multipart/form-data;') ,
 
CURLOPT_HTTPHEADER => array('Content-Type: multipart/form-data;') ,
 
CURLOPT_POST => true,
 
CURLOPT_POST => true,
Line 150: Line 149:
 
$result = curl_exec($ch);
 
$result = curl_exec($ch);
 
curl_close($ch);
 
curl_close($ch);
 +
 
return $result;
 
return $result;
 +
 
}
 
}
  
 +
// Create an instance of job
 
$job = new Job();
 
$job = new Job();
  
 +
// Configure the job how you like it using getters and setters. Each method called here can do the heavy lifting of validation/sanitisation
 +
// For example you may want to validate the user exists in your database, that the date is in the future or that the address_1 isn’t over 45 chars
 +
$job->setMobileUser('YOUR_USER'); // SET ME
 +
$job->setDateAndTime("2015-10-10","15:55:00"); // SET ME TO TODAY
 +
$job->setJobType("PODC");
 +
$job->randomJobNumber();
 
$jobsJSON = $job->getJSON();
 
$jobsJSON = $job->getJSON();
−
 
+
 
$result = sendJob($jobsJSON,'1/jobs');
 
$result = sendJob($jobsJSON,'1/jobs');
 
echo $result;
 
echo $result;
  
 +
?>
 +
 +
 +
<?
 +
/**
 +
* Copy this into a new file called config.php and use it for storing static variables
 +
*
 +
* Once you have done that add include('/path/to/config.php'); at the top of send.php (like jobs is at the moment!)
 +
*/
 +
class config {
 +
    public static $url = "http://live.mymobileworkers.com:8080/api/";
 +
    public static $apiKey = "VALID_API_KEY"; // SET ME
 +
}
 +
 +
/*
 +
* End copy
 +
*/
  
  
 
</source>
 
</source>

Revision as of 08:30, 9 October 2015

Since PHP has built in functions to encode/decode JSON it provides a quick and easy method to test the API.

Creating a job

Using OOP is the most sensible approach so create a class called file called Job.php

Job.php

<?php
 
class Job {
 
	public $job_number;
	public $address_1;
	public $address_2;
	public $contact_name;
	public $contact_number_1;
	public $contact_number_2;
	public $mobile_user; //username not id
	public $postcode;
	public $type = 'PODC';//job type in mmw - this is case sensitive and depends what workflows you have setup.
   public $date;
   public $time;
	public $special_instructions;
	public $repair_description;
 
    /**
     * Construct can be used to set static values, however most stuff will be moved out of here into "getters and setters" that are below
     */
function __construct(){
 
//Here you could pass an ID and populate the job from a mysql record etc..
 
$this->address_1="23 The parks";
$this->postcode="WA12 0JQ";
$this->contact_name = "Joe Bloggs";
 
 
}
 
    /**
     * @param $date
     * @param $time
     *
     * Function that allows setting of job date and time,
     *
     * Both are optional fields, passing null for a date or time will prevent the value being set in MMW
     */
public function setDateAndTime($date,$time){
     if ($date){
        $this->date = $date;
     }
    if($time) {
       $this->time = $time;
    }
}
 
    /**
     * @param $type
     * Default job type is "Repair" but you will have custom templates in your system
     *
     * You should always try to validate as much at this side as you can so this setter uses a case statement to ensure we only set allowed values
     */
public function setJobType($type){
    switch($type){
        case "Repair":
            $this->type = "Repair";
            break;
        case "PODC":
            $this->type = "PODC";
            break;
 
    }
}
 
    /**
     * @param $username
     *
     * Sets the optional username field in MMW
     * Note: If the username doesnt exist it will fail, ending no username is acceptable by passing null to this method
     */
public function setMobileUser($username){
    if($username){
        $this->mobile_user = array('username' => $username);
    } else {
        $this->mobile_user = null;
    }
}
 
    /**
     * Method that generates a random job number.. just for testing!
     */
public function randomJobNumber(){
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < 10; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
 
    $this->job_number = $randomString;
}
    /**
     * @return string
     *
     * Responsible for bundling the class into MMW shaped data for curling to the API
     */
function getJSON(){
	// we have a container array so the structure matches what the API expects
	// we send 1 job at a time but the api can handle an array of jobs. We send an array ($container) of 1 job basically.
		$jobs = array();
		$container = array();
		$container[] =  array_filter((array) $this); // The API can be a bit funny about null values, this field strips the null values out of the job class
		$jobs['jobs'] = $container;
		return json_encode($jobs);
 
	}
 
}
?>

Send.php

<?php
 
include('job.php');
//TODO include('config.php');
 
 
function sendJob($json,$method){	
		// Initializing curl
 
        $url = config::$url;
        $apiKey = config::$apiKey;
		$ch = curl_init( $url . $method );
		$options = array(
 
				CURLOPT_HTTPHEADER => array('Content-Type: multipart/form-data;') ,
				CURLOPT_POST => true,
				CURLOPT_POSTFIELDS => array('api_key' => $apiKey,'file' => $json,'update_existing' => 'true'),
				CURLOPT_RETURNTRANSFER => true,
		);
		// Setting curl options
		curl_setopt_array( $ch, $options ) ;
		$result = curl_exec($ch);
		curl_close($ch);
 
		return $result;
 
	}
 
// Create an instance of job
$job = new Job();
 
// Configure the job how you like it using getters and setters. Each method called here can do the heavy lifting of validation/sanitisation 
// For example you may want to validate the user exists in your database, that the date is in the future or that the address_1 isn’t over 45 chars
$job->setMobileUser('YOUR_USER'); // SET ME
$job->setDateAndTime("2015-10-10","15:55:00"); // SET ME TO TODAY
$job->setJobType("PODC");
$job->randomJobNumber();
$jobsJSON = $job->getJSON();
 
$result = sendJob($jobsJSON,'1/jobs');
echo $result;
 
?>
 
 
<?
/**
 * Copy this into a new file called config.php and use it for storing static variables
 *
 * Once you have done that add include('/path/to/config.php'); at the top of send.php (like jobs is at the moment!)
 */
class config {
    public static $url = "http://live.mymobileworkers.com:8080/api/";
    public static $apiKey = "VALID_API_KEY"; // SET ME
}
 
/*
 * End copy
 */
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox