Difference between revisions of "PHP"

From MyMobileWorkers API
Jump to: navigation, search
(→‎Job.php)
(→‎Job.php)
Line 7: Line 7:
 
=== Job.php ===
 
=== Job.php ===
 
<source lang="php">
 
<source lang="php">
 +
<?php
  
 
class Job {
 
class Job {
−
 
+
 
public $job_number;
 
public $job_number;
 
public $address_1;
 
public $address_1;
Line 16: Line 17:
 
public $contact_number_1;
 
public $contact_number_1;
 
public $contact_number_2;
 
public $contact_number_2;
−
public $mobile_user = array();//username not id
+
public $mobile_user; //username not id
 
public $postcode;
 
public $postcode;
−
public $type = 'repair';//job type in mmw
+
public $type = 'PODC';//job type in mmw - this is case sensitive and depends what workflows you have setup.
−
public $timeslot;
+
  public $date;
 +
  public $time;
 
public $special_instructions;
 
public $special_instructions;
 
public $repair_description;
 
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(){
 
function __construct(){
−
 
+
 
//Here you could pass an ID and populate the job from a mysql record etc..
 
//Here you could pass an ID and populate the job from a mysql record etc..
  
−
$this->job_number = "TEST_JOB01";
+
$this->address_1="23 The parks";
−
$this->address_1 = "ADDRESS LINE 1";
+
$this->postcode="WA12 0JQ";
−
-------- populate rest of the fields here
+
$this->contact_name = "Joe Bloggs";
−
$this->repair_description = "Faulty whatsit";
+
 
−
$this->mobile_user['username'] = "BOB"
+
  
 
}
 
}
  
 +
    /**
 +
    * @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(){
 
function getJSON(){
 
// we have a container array so the structure matches what the API expects
 
// we have a container array so the structure matches what the API expects
Line 40: Line 111:
 
$jobs = array();
 
$jobs = array();
 
$container = array();
 
$container = array();
−
$container[] = $this;
+
$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;
 
$jobs['jobs'] = $container;
−
 
 
return json_encode($jobs);
 
return json_encode($jobs);
−
 
+
 
}
 
}
−
 
  
 
}
 
}
−
 
+
?>
−
/* Optional getters/setters could be here */
+
  
  

Revision as of 08:27, 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

include('Job.php');
 
 
/* This is the file you execute */
 
 
//settings 
$url = "http://live.mymobileworkers.com:8080/api/";
$apiKey = "ENTER HERE";
 
function sendJob($json,$method){	
		// Initializing curl
		$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;
	}
 
$job = new Job();
 
$jobsJSON = $job->getJSON();
 
$result = sendJob($jobsJSON,'1/jobs');
echo $result;
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox