Difference between revisions of "PHP"
From MyMobileWorkers API
(→Send.php) |
|||
| (16 intermediate revisions by 3 users not shown) | |||
| Line 1: | Line 1: | ||
| − | Since PHP | + | Since PHP has built in functions to encode/decode JSON it provides a quick and easy method to test the API. |
== Creating a job == | == Creating a job == | ||
| − | Using OOP is the most sensible approach so create a class | + | Using OOP is the most sensible approach so create a class file called Job.php |
=== 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 | + | public $mobile_user; //username not id |
public $postcode; | public $postcode; | ||
| − | public $type = ' | + | public $type = 'Repair';//job type in mmw - this is case sensitive and depends what workflows you have setup. |
| − | public $ | + | 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->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(){ | function getJSON(){ | ||
| − | // we have a container array so the structure what the API expects | + | // 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. | // 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(); | $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); | ||
| − | + | ||
} | } | ||
| − | |||
} | } | ||
| − | |||
| − | |||
| − | |||
</source> | </source> | ||
| Line 58: | Line 124: | ||
<source lang="php"> | <source lang="php"> | ||
| − | + | <?php | |
| + | include('job.php'); | ||
| + | //TODO include('config.php'); | ||
| − | |||
| + | function sendJob($json,$method){ | ||
| + | // Initializing curl | ||
| − | + | $url = config::$url; | |
| − | $url = | + | $apiKey = config::$apiKey; |
| − | $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 82: | Line 147: | ||
$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 = | + | $result = sendJob($jobsJSON,'1/jobs'); |
echo $result; | echo $result; | ||
| + | ?> | ||
| + | |||
| + | |||
| + | <?php | ||
| + | /** | ||
| + | * 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://api.mymobileworkers.com:8080/api/"; | ||
| + | public static $apiKey = "VALID_API_KEY"; // SET ME | ||
| + | } | ||
| + | |||
| + | /* | ||
| + | * End copy | ||
| + | */ | ||
</source> | </source> | ||
Latest revision as of 12:28, 14 November 2016
Since PHP has built in functions to encode/decode JSON it provides a quick and easy method to test the API.
[edit] Creating a job
Using OOP is the most sensible approach so create a class file called Job.php
[edit] 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 = 'Repair';//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); } }
[edit] 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; ?> <?php /** * 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://api.mymobileworkers.com:8080/api/"; public static $apiKey = "VALID_API_KEY"; // SET ME } /* * End copy */