Skip to content

Instantly share code, notes, and snippets.

@peterjaap
Last active June 4, 2021 03:08
Show Gist options
  • Save peterjaap/057b22110f2f0f17d7c48510a64323b1 to your computer and use it in GitHub Desktop.
Save peterjaap/057b22110f2f0f17d7c48510a64323b1 to your computer and use it in GitHub Desktop.
Packit4me 3d viz
<?php
$url = "http://www.packit4me.com/api/call/raw";
$previewUrl = "http://www.packit4me.com/api/call/preview";
$bin = [
'id' => 0,
'maxweight' => 50,
'dimensions' => '5x5x5'
];
$items = [];
$items[] = [
'binId' => 0,
'id' => 0,
'weight' => 15,
'dimensions' => '1x1x1',
];
$items[] = [
'binId' => 0,
'id' => 1,
'weight' => 15,
'dimensions' => '3x3x3',
];
$items[] = [
'binId' => 0,
'id' => 2,
'weight' => 5,
'dimensions' => '2x5x1',
];
$itemStringParts = [];
foreach ($items as $item) {
$itemStringParts[] = implode(':', $item);
}
$itemString = implode(',', $itemStringParts);
$data = 'bins=' . implode(':', $bin) . '&items=' . $itemString;
$previewData = $data . '&binId=' . $bin['id'];
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with url");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from url");
}
$previewParams = array('http' => array(
'header' => "Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: ".strlen($previewData)."\r\n".
"User-Agent:peterjaap-packit4me/1.0\r\n",
'method' => 'POST',
'content' => $previewData
));
$ctx = stream_context_create($previewParams);
$fp = fopen($previewUrl, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with url");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from url");
}
echo str_replace('/resources/', 'http://www.packit4me.com/resources/', $response);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment