Set options when uploading model via API in PHP


(fabian) #1

dear friends,

I am trying to set the "options" when uploading to /v3/models.
the file gets uploaded ok, with all fields working, except "options".
maybe you can help me.

here is my code in PHP:

<?php
$name = $_GET['name'];
$file = "C:/xampp/htdocs/tmp/".$name.'.zip';

$bg_uid = '51af6a870cce449eb75b0345feebaebb';
$env_uid = '3e2ad3e1f1ea47679f045a7eb0e6af49';

$bg = array('image' => $bg_uid);
$orient = array('axis' => '[1, 0, 0]', 'angle' => '80');

$options = array(
"background" => $bg,
"orientation" => $orient
);

$params = array(
"name" => $name,
"description" => 'formfreiheit by schwarzlichtfabrik.de. Shapes are made in a VR App.',
"modelFile" => new CurlFile($file),
"options" => json_encode($options),
"token" => 'TOKEN'
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_URL, "https://api.sketchfab.com/v2/models");
$result = curl_exec($ch);
if($result === false)
echo curl_error($ch);
else
print_r($result) ;

?>


(Tribble42) #2

Hey, it looks like you're uploading on our V2 endpoint, not V3.
V2 has no support for options during upload.

The V3 will tell you if the options you're sending are incorrect in the POST /v3/models body response.

Please note though that as options are an optional parameters, POST /v3/models will not return 400 if you provide invalid content, and instead process the model without custom options.


(fabian) #3

just to complete this thread, here is my working code.
maybe that helps someone with PHP and set options, background, enviroment and orientation.

<?php
$bg_uid = '51af6a870cce449eb75b0345feebaebb';
$env_uid = '3e2ad3e1f1ea47679f045a7eb0e6af49';
$bg = array('image' => $bg_uid);
$orient = '{"axis": [1, 0, 0], "angle": 5}';
$options = array(
    "background" => json_encode($bg),
    "environment" => $env_uid
    "orientation" => $orient
);

$params = array(
    "name" => 'The Models Name',
    "description" => '3D Printable Vase and Lampshades.<br>Prints best in "vase mode" or "spiral mode", depending on your printing software. <br><br>Created with ShapeLab by schwarzlichtfabrik.de. Made in Virtual Reality.',
    "modelFile" => new CurlFile($file),
    "license" => 'by-nc-sa',
    "options" => json_encode($options),
    "token" => '', // <-- Enter your API Token here
    "categories" => 'art-abstract'
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false); 
curl_setopt($ch, CURLOPT_URL, "https://api.sketchfab.com/v3/models");
$result = curl_exec($ch);
if($result === false) {
    echo '<br>### curl error!: '.curl_error($ch);
}

$uid = json_decode($result);
?>