Created
January 15, 2015 16:01
-
-
Save dsugarman/437c4c91aea86860ed13 to your computer and use it in GitHub Desktop.
Jet PHP API Template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** To use this just: | |
1. put in your api information at the top | |
2. store/retreive your API token in the specified locations | |
3.store your api calls | |
Ex: $jet->uploadFile($feed_type, $path); | |
$jet->processOrdersByStatus('ready'); | |
$jet->apiPUT("/orders/".$JET_ORDER_ID."/acknowledge", $data); | |
**/ | |
class Jet | |
{ | |
protected static $api_user_id = "your api user id"; | |
protected static $api_secret = "your api secret" | |
protected static $merchant_id = "your merchant id"; | |
protected static $api_prefix = 'https://merchant-api.jet.com/api/'; | |
/** | |
* Refresh Jet API token | |
**/ | |
public function getNewToken(){ | |
$ch = curl_init($this::$api_prefix.'/Token'); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
//If necessary add your ssl pem: curl_setopt($ch, CURLOPT_CAINFO,'/ssl/cacert.pem'); | |
$request = json_encode(array( | |
"user" => $this::$api_user_id, | |
"pass" => $this::$api_secret | |
)); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $request); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
'Content-Type: application/json', | |
'Content-Length: ' . strlen($request)) | |
); | |
$data = curl_exec($ch); | |
curl_close($ch); | |
if($data = json_decode($data)){ | |
if($token = $data->id_token){ | |
//SAVE $token SOMEWHERE and save last time you got a token | |
$this->setToken($token); | |
$this->setTokenTs(date('r')); | |
$this->save(); | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* Upload bulk file to Jet | |
**/ | |
public function uploadFile($type, $path){ | |
$data = $this->apiGET('files/uploadToken'); | |
$url = $data->url; | |
$file_id = $data->jet_file_id; | |
$this->apiFilePUT($url, $path, $file_id); | |
$this->apiGET('files/uploaded', array("url" => $url, "file_type" => $type, "file_name" => basename($path).".gz")); | |
} | |
/** | |
* PUT request to $url | |
**/ | |
public function apiPUT($end_point, $request){ | |
if(substr($end_point,0,1) === "/") $end_point = substr($end_point,1); | |
//get token if it has been over 9 hours since the last token | |
//CHANGE THIS TO WHERE YOU SAVED THE TOKEN AND TS | |
if(round((strtotime("now") - strtotime($this->getTokenTs()))/3600, 1) > 9) $this->getNewToken(); | |
$api_call_data = array(); | |
$api_call_data["request_ts"] = date("r", strtotime("now")); | |
$ch = curl_init($this::$api_prefix . $end_point); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
//If necessary add your ssl pem: curl_setopt($ch, CURLOPT_CAINFO,'/ssl/cacert.pem'); | |
$request = json_encode($request); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
'Content-Type: application/json', | |
'Content-Length: ' . strlen($request), | |
//CHANGE THIS TO WHERE YOU SAVED YOUR TOKEN | |
'Authorization: Bearer ' . $this->getToken() ) | |
); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); | |
curl_setopt($ch, CURLOPT_POSTFIELDS,$request); | |
$data = curl_exec($ch); | |
echo (curl_error ($ch )); | |
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_close($ch); | |
$api_call_data["request_data"] = (string)var_export($request, true); | |
$api_call_data["response_ts"] = date("r", strtotime("now")); | |
$api_call_data["response_data"] = (string)var_export($data, true); | |
$api_call_data["request_url"] = $this::$api_prefix . $end_point; | |
$api_call_data["service_type"] = "Jet"; | |
$api_call_data["status_code"] = $httpcode; | |
//SAVE $api_call_data SOMEWHERE | |
return json_decode($data); | |
} | |
/** | |
* PUT request to $url | |
**/ | |
public function apiFilePUT($url, $path, $file_id = null){ | |
//get token if it has been over 9 hours since the last token | |
//CHANGE THIS TO WHERE YOU SAVED THE TOKEN AND TS | |
if(round((strtotime("now") - strtotime($this->getTokenTs()))/3600, 1) > 9) $this->getNewToken(); | |
$api_call_data = array(); | |
$api_call_data["request_ts"] = date("r", strtotime("now")); | |
//gzip the data | |
$file = file_get_contents($path); | |
$gz_file = gzencode($file,9); | |
$g_path = $path.".gz"; | |
$gfp = fopen($g_path, 'w+'); | |
fwrite($gfp, $gz_file); | |
rewind($gfp); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_PUT, 1); | |
//If necessary add your ssl pem: curl_setopt($ch, CURLOPT_CAINFO,'/ssl/cacert.pem'); | |
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'x-ms-blob-type: BlockBlob' ) ); | |
curl_setopt($ch, CURLOPT_INFILE, $gfp); | |
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($g_path)); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
$data = curl_exec ($ch); | |
fclose($gfp); | |
//delete the file / gzip file | |
//unlink($path); | |
unlink($g_path); | |
$api_call_data["response_ts"] = date("r", strtotime("now")); | |
$api_call_data["response_data"] = (string)var_export($data, true); | |
$api_call_data["request_url"] = $url; | |
$api_call_data["service_type"] = "Jet"; | |
$api_call_data["check_status"] = true; | |
if($file_id) $api_call_data["service_id"] = $file_id; | |
//SAVE $api_call_data | |
return $data; | |
} | |
/** | |
* Jet API GET | |
**/ | |
public function apiGET($end_point, $request = null){ | |
if(substr($end_point,0,1) === "/") $end_point = substr($end_point,1); | |
//get token if it has been over 9 hours since the last token | |
//CHANGE THIS TO WHERE YOU SAVED THE TOKEN AND TS | |
if(round((strtotime("now") - strtotime($this->getTokenTs()))/3600, 1) > 9) $this->getNewToken(); | |
$api_call_data = array(); | |
$api_call_data["request_ts"] = date("r", strtotime("now")); | |
$ch = curl_init($this::$api_prefix . $end_point); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer ' . $this->getToken() ) ); | |
//If necessary add your ssl pem: curl_setopt($ch, CURLOPT_CAINFO,'/ssl/cacert.pem'); | |
if($request){ | |
$request = json_encode($request); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $request); | |
$api_call_data["request_data"] = (string)var_export($request, true); | |
} | |
$data = utf8_encode (curl_exec($ch)); | |
echo (curl_error ($ch )); | |
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_close($ch); | |
$api_call_data["response_ts"] = date("r", strtotime("now")); | |
$api_call_data["response_data"] = (string)var_export($data, true); | |
$api_call_data["request_url"] = $this::$api_prefix . $end_point; | |
$api_call_data["service_type"] = "Jet"; | |
$api_call_data["status_code"] = $httpcode; | |
//SAVE $api_call_data | |
return json_decode($data); | |
} | |
/** | |
* Poll for orders | |
**/ | |
public function processOrdersByStatus($status){ | |
$data = $this->apiGET("orders/$status"); | |
foreach($data->order_urls as $end_point){ | |
$this->processOrder($end_point); | |
} | |
} | |
/** | |
* Process Order | |
**/ | |
public function processOrder($end_point){ | |
$data = $this->apiGET($end_point); | |
//STORE AND PROCESS $data | |
} | |
} |
there is error in your json block mention in the error., it because the json structure have been changed, please updte it.
Thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the code. I have generated token key using your code but I am unable to upload product because of wrong array format. Can you please suggest what should be the array format ? I tried diffrent types of array format refering this link https://developer.jet.com/docs/services/5565ca949a274a12b0b3a2a3/operations/5565d4be9a274a12b0b3a2ae. I am getting below error
{ "errors": [ "Expected Array\n {minItems = Some 1;\n maxItems = null;\n items =\n ({description = null;\n required = false;},\n Object\n {title = "ProductCodes";\n properties =\n [("standard_product_code",\n ({description =\n Some\n "A standard, unique identifier for a product. ISBN-10, ISBN-13, UPC, EAN and GTIN-14 are accepted.\r\n\r\n\t\t\t\t\t#Valid Values\r\n\r\n\t\t\t\t\t_If standard_product_code_type is'UPC' - must be 12 digits\r\n\r\n\t\t\t\t\t_If standard_product_code_type is 'ISBN-13' - must be 13 digits\r\n\r\n\t\t\t\t\t_If standard_product_code_type is 'ISBN-10' - must be 10 digits\r\n\r\n\t\t\t\t\t_If standard_product_code_type is 'EAN' - must be 13 digits\r\n\r\n\t\t\t\t\t_If standard_product_code_type is 'GTIN-14' - must be 14 digits\r\n\r\n\t\t\t\t\t";\n required = true;}, String {maxLength = Some 14;\n minLength = null;\n enum = null;\n format = null;\n validate = true;}));\n ("standard_product_code_type",\n ({description =\n Some\n "Indicate the type of standard_product_code that was used.\r\n\r\n\t\t\t\t\t#Valid Values\r\n\r\n\t\t\t\t\t_UPC\r\n\r\n\t\t\t\t\t_ISBN-13\r\n\r\n\t\t\t\t\t_ISBN-10\r\n\r\n\t\t\t\t\t_EAN\r\n\r\n\t\t\t\t\t_GTIN-14\r\n\r\n\t\t\t\t\t";\n required = true;},\n String\n {maxLength = null;\n minLength = null;\n enum =\n Some ["UPC-E"; "UPC"; "ISBN-13"; "ISBN-10"; "EAN"; "GTIN-14"];\n format = null;\n validate = true;}))];});}, got {\r\n "standard_product_code": "1234567890467",\r\n "standard_product_code_type": "UPC"\r\n}" ] }
Please help. I am stuck in this from last week & half.
Thanks.