Difference between revisions of "PHP"
From MyMobileWorkers API
| Line 1: | Line 1: | ||
Since PHP as built in functions to encode/decode JSON it provides a quick and easy method to test the API. | Since PHP as built in functions to encode/decode JSON it provides a quick and easy method to test the API. | ||
| − | Using OOP is the most sensible approach so create a class called Job. | + | == Creating a job == |
| + | |||
| + | Using OOP is the most sensible approach so create a class called file called Job.php | ||
=== Job.php === | === Job.php === | ||
| Line 27: | Line 29: | ||
$job_number = "TEST_JOB01"; | $job_number = "TEST_JOB01"; | ||
$address_1 = "ADDRESS LINE 1"; | $address_1 = "ADDRESS LINE 1"; | ||
| − | -------- populate further records | + | -------- populate further records here! |
$repair_description = "Faulty whatsit"; | $repair_description = "Faulty whatsit"; | ||
| Line 47: | Line 49: | ||
} | } | ||
| + | |||
| + | /* Optional getters/setters could be here */ | ||
| + | |||
| + | |||
| + | </source> | ||
| + | |||
| + | === Send.php === | ||
| + | |||
| + | <source lang="php"> | ||
| + | include('Job.php'); | ||
| + | |||
| + | |||
| + | /* This is the file you execute */ | ||
| + | |||
| + | |||
| + | //settings | ||
| + | $url = "http://live.mymobileworkers.com/api/"; | ||
| + | $apiKey = "ENTER HERE"; | ||
| + | |||
| + | public 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 = $this->sendJob($jobsJSON,'jobs') | ||
| + | echo $result; | ||
| + | |||
| + | |||
</source> | </source> | ||
Revision as of 15:15, 14 May 2013
Since PHP as 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.. $job_number = "TEST_JOB01"; $address_1 = "ADDRESS LINE 1"; -------- populate further records here! $repair_description = "Faulty whatsit"; } function getJSON(){ // we have a container array so the structure 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/api/"; $apiKey = "ENTER HERE"; public 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 = $this->sendJob($jobsJSON,'jobs') echo $result;