REST image upload not working

pepsis

Joined: 2013-11-13
Posts: 2
Posted: Wed, 2013-11-13 14:49

I'm trying to upload images to my gallery install with the following code and am getting the following error.

Can anyone point me in the right direction?
<?php

$APIBASE = 'http://localhost/gallery3/index.php/rest';
$APIKEY = '<key_goes_here>';
die(var_dump(add_picture('test.jpg', 7)));

function add_picture($filename, $parentid = 1) {
GLOBAL $APIKEY, $APIBASE;
$args = array();
$ch = curl_init($APIBASE.'/item/7');
$ent = new stdClass();
$file = new stdClass();
$ent->name = $filename;
$ent->type = 'photo';
$ent->title = "Title Goes Here";
$file->filename = $filename;

$curlOpts = array(
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
'X-Gallery-Request-Method: POST',
'X-Gallery-Request-Key: '.$APIKEY),
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POSTFIELDS => array('entity' => json_encode($ent), 'file' => $filename)
);

curl_setopt_array($ch, $curlOpts);
$x = curl_exec($ch) or die(curl_errno($ch));
return $x;
}

/*
Response is
string '{"errors":{"file":{}}}' (length=22)
*/

 
pepsis

Joined: 2013-11-13
Posts: 2
Posted: Wed, 2013-11-13 23:49

I've now found the bug and now have a working example.

function add_picture($filename, $parentid = 1) {
GLOBAL $APIKEY, $APIBASE;
$args = array();
$ch = curl_init($APIBASE);
$ent = new stdClass();
$file = new stdClass();
$ent->name = $filename;
$ent->type = 'photo';
$file = $filename;

$curlOpts = array(
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
'X-Gallery-Request-Method: post',
'X-Gallery-Request-Key: '.$APIKEY),
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POSTFIELDS =>
// because this is an array, a Content-Type: miltipart/form-data
array('entity' => json_encode($ent), 'file' => '@'.realpath($file))
);

curl_setopt_array($ch, $curlOpts);
$x = curl_exec($ch) or die('CURLERROR:['.curl_error($ch)."]\n");
return $x;
}