Sindbad~EG File Manager
<?php
/*
//next example will recieve all messages for specific conversation
$service_url = 'https://merchant-api.jet.com/api/token';
$curl = curl_init($service_url);
$curl_post_data = array(
'user' => '6A03D4378C22926F144BDB3C1057DEB6B7B91DC2',
'pass' => 'b8uaY4ZpBVVWza7guZqR9aDZnYlvK5MkrjUZy3awfm7Z'
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
$decoded = json_decode($curl_response);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
die('error occured: ' . $decoded->response->errormessage);
}
echo 'response ok!'.$curl_response;
var_export($decoded->response);
*/
if($_GET['step'] == '1') {
$data = array("user" => "7C9157022E885BB7312D4223BD607BF7E212EBC7", "pass" => "eUyJrPgj5MVDwzprcE1nfGWOBzKsYUOn34iNyP1k9MS4");
$data_string = json_encode($data);
$ch = curl_init('https://merchant-api.jet.com/api/token');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
echo $result;
} else if($_GET['step'] == '2') {
$data = array("fulfillment_nodes" => "d7efb83abe5f4f688a3c1624d21e56a5", "quantity" => "1000");
$data_string = json_encode($data);
$ch = curl_init('https://merchant-api.jet.com/api/merchant-skus/737dbaab01324c739a09cc563ff4c9c5/Inventory');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
echo $result;
}
?>
<?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 = "7C9157022E885BB7312D4223BD607BF7E212EBC7";
protected static $api_secret = "eUyJrPgj5MVDwzprcE1nfGWOBzKsYUOn34iNyP1k9MS4";
protected static $merchant_id = "3def76a484e64453b78b5be07c420f0c";
protected static $api_prefix = "https://merchant-api.jet.com/api/token";
/**
* Refresh Jet API token
**/
public function getNewToken() {
$ch = curl_init($api_prefix);
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" => $api_user_id,
"pass" => $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);
echo $data;
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();
echo('test');
return true;
}
}
echo('test');
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
}
}
$jets = new Jet();
echo "Get new token:";
$jets->getNewToken();
?>
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists