serviceURL = 'http://adpdemo.pperfect.com/ecomService/v4/Json/'; $this->username = ''; //enter your username $this->password = ''; //enter your password $this->token = ''; $this->json = new Services_JSON(); } //this function makes a call to the ecom webservivce using json encoded parameters and returns the result //this particular example uses curl but a regular php "file_get_contents" call can also be used function makeCall($class, $method, $params, $token = null) { $jsonParams = $this->json->encode($params); $serviceCall = $this->serviceURL.'?params='.urlencode($jsonParams)."&method=$method&class=$class"; if ($token != null) { $serviceCall.='&token_id='.$token; } echo "
\nCALL: $serviceCall
\n
\n"; $session = curl_init($serviceCall); curl_setopt($session, CURLOPT_HEADER, false); curl_setopt($session, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($session); curl_close($session); /*echo "
\nRAW RESPONSE
\n
\n"; print_r($response); echo "
\n
\n";*/ return $this->json->decode($response); } /* This example will go through: * 1) getting places for collection and delivery * 2) submitting the quote and receiving the service options * 3) selecting a service * 4) converting the quote to a waybill*/ function runJsonExample() { //get the salt $params = array(); $saltParams['email'] = $this->username; $response = $this->makeCall('Auth','getSalt',$saltParams); print_r($response); //check for error if ($response->errorcode == 0) { //no error $salt = $response->results[0]->salt; echo "Salt: ".$salt."
\n"; } else { //error, display notice and quit echo "Error: ".$response->errormessage; die; } //get the token $md5pass = md5($this->password.$salt); //echo "md5pass $md5pass
\n"; $tokenParams = array(); $tokenParams['email'] = $this->username; $tokenParams['password'] = $md5pass; $response = $this->makeCall('Auth','getSecureToken',$tokenParams); //check for error if ($response->errorcode == 0) { //no error $this->token = $response->results[0]->token_id; echo "Token: ".$this->token."
\n
\n"; } else { //error, display notice and quit echo "Error: ".$response->errormessage; die; } $available_postal_codes = array(); $postCodeLookupParams = array(); $postCodeLookupParams['postcode'] = '6730'; $postCodeLookupResponse = $this->makeCall('Quote','getPlacesByPostcode',$postCodeLookupParams, $this->token); echo "
\n"; print_r($postCodeLookupResponse); echo "
\n"; //originating location details $nameLookupParams['name'] = 'Johan'; $nameLookupResponse = $this->makeCall('Quote','getPlacesByName',$nameLookupParams, $this->token); echo "
\n"; print_r($nameLookupResponse); echo "
\n"; //build quote request: $quoteParams = array(); $quoteParams['details'] = array(); //i added these just to make sure these tests are not processed as actual waybills $quoteParams['details']['specinstruction'] = "This is a test"; $quoteParams['details']['reference'] = "This is a test"; //originating location $quoteParams['details']['origperadd1'] = 'Address line 1'; $quoteParams['details']['origperadd2'] = 'Address line 2'; $quoteParams['details']['origperadd3'] = 'Address line 3'; $quoteParams['details']['origperadd4'] = 'Address line 4'; $quoteParams['details']['origperphone'] = '012345678'; $quoteParams['details']['origpercell'] = '012345678'; //i used the 1st result from the list returned when looking up postcode = 3340 as there was only 1, but normally the user would choose $quoteParams['details']['origplace'] = $postCodeLookupResponse->results[0]->place; $quoteParams['details']['origtown'] = $postCodeLookupResponse->results[0]->town; $quoteParams['details']['origpers'] = 'TESTCUSTOMER'; $quoteParams['details']['origpercontact'] = 'origcontactsname'; $quoteParams['details']['origperpcode'] = '6730'; //postal code //destination location details $quoteParams['details']['destperadd1'] = 'Address line 1'; $quoteParams['details']['destperadd2'] = 'Address line 2'; $quoteParams['details']['destperadd3'] = 'Address line 3'; $quoteParams['details']['destperadd4'] = 'Address line 4'; $quoteParams['details']['destperphone'] = '012345678'; $quoteParams['details']['destpercell'] = '012345678'; //i chose the 1st result, but this will be up to the user as above $quoteParams['details']['destplace'] = $nameLookupResponse->results[0]->place; $quoteParams['details']['desttown'] = $nameLookupResponse->results[0]->town; $quoteParams['details']['destpers'] = 'TESTCUSTOMER'; $quoteParams['details']['destpercontact'] = 'destcontactsname'; $quoteParams['details']['destperpcode'] = '3340'; //postal code /* Add the contents: * There needs to be at least 1 contents item with an "actmass" > 0 otherwise a rate will not calculate. * "Contents" needs to be an array object, even if there is only one contents item. */ //Create contents array object $quoteParams['contents'] = array(); //Create first contents item (index 0 in the contents array) $quoteParams['contents'][0] = array(); //Add contents details $quoteParams['contents'][0]['item'] = 1; $quoteParams['contents'][0]['desc'] = 'this is a test'; $quoteParams['contents'][0]['pieces'] = 1; $quoteParams['contents'][0]['dim1'] = 1; $quoteParams['contents'][0]['dim2'] = 1; $quoteParams['contents'][0]['dim3'] = 1; $quoteParams['contents'][0]['actmass'] = 1; //Create second contents item (index 1 in the contents array) $quoteParams['contents'][1] = array(); //Add contents details $quoteParams['contents'][1]['item'] = 2; $quoteParams['contents'][1]['desc'] = 'ths is another test'; $quoteParams['contents'][1]['pieces'] = 1; $quoteParams['contents'][1]['dim1'] = 1; $quoteParams['contents'][1]['dim2'] = 1; $quoteParams['contents'][1]['dim3'] = 1; $quoteParams['contents'][1]['actmass'] = 1; echo "
\n
\n ---- request params ----
\n"; print_r($quoteParams); echo "
\n
\n ---- making request ----
\n"; $quoteResponse = $this->makeCall('Quote','requestQuote',$quoteParams, $this->token); echo "
\n
\n ---- response ----
\n
\n"; print_r($quoteResponse); if ($response->errorcode !== 0) { echo "Error: ".$response->errormessage; die; } /* * the user then needs to choose the service most desirable to them and use * the "updateService" method to set the service, * thereafter use the "quoteToWaybill" or "quoteTocCollection" method convert the quote to a legitimate waybill or collection * * */ $updateServiceParams = array(); $updateServiceParams['quoteno'] = $quoteResponse->results[0]->quoteno; $updateServiceParams['service'] = $quoteResponse->results[0]->rates[0]->service; //i used the first 1 returned $updateResponse = $this->makeCall('Quote','updateService',$updateServiceParams, $this->token); echo "
\n--------
\nFinal Rates:
\n
\n"; print_r($updateResponse); //Convert quote to waybill //Calling this method will create a waybill with the same details as the submitted quote $convertQuoteToWaybillParams = array(); $convertQuoteToWaybillParams['quoteno'] = $quoteResponse->results[0]->quoteno; //this parameter is MANDATORY $convertQuoteToWaybillParams['specins'] = "special instructions"; //this parameter is OPTIONAL echo "
\n
\n ---- convert params ----
\n"; print_r($convertQuoteToWaybillParams); $convertResponse = $this->makeCall('Quote','quoteToWaybill',$convertQuoteToWaybillParams, $this->token); echo "
\n
\n ---- response ----
\n
\n"; print_r($convertResponse); } } $ex = new Example(); $ex->runJsonExample(); ?>