Difference between revisions of "PHP"
From MyMobileWorkers API
(→Send.php) |
(→Job.php) |
||
| Line 27: | Line 27: | ||
//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.. | ||
| − | $job_number = "TEST_JOB01"; | + | $this->job_number = "TEST_JOB01"; |
| − | $address_1 = "ADDRESS LINE 1"; | + | $this->address_1 = "ADDRESS LINE 1"; |
-------- populate rest of the fields here | -------- populate rest of the fields here | ||
| − | $repair_description = "Faulty whatsit"; | + | $this->repair_description = "Faulty whatsit"; |
| − | $mobile_user['username'] = "BOB" | + | $this->$mobile_user['username'] = "BOB" |
} | } | ||
Revision as of 16:10, 8 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
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 = array();//username not id public $postcode; public $type = 'repair';//job type in mmw public $timeslot; public $special_instructions; public $repair_description; function __construct(){ //Here you could pass an ID and populate the job from a mysql record etc.. $this->job_number = "TEST_JOB01"; $this->address_1 = "ADDRESS LINE 1"; -------- populate rest of the fields here $this->repair_description = "Faulty whatsit"; $this->$mobile_user['username'] = "BOB" } 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[] = $this; $jobs['jobs'] = $container; return json_encode($jobs); } } /* Optional getters/setters could be here */
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;