Announcement

  •  » Extensions
  •  » Unable to upload image using pwg.images.addSimple in PHP

#1 2022-04-06 15:41:05

colorpencil
Member
2022-03-25
4

Unable to upload image using pwg.images.addSimple in PHP

Hello,

I am trying to upload an image using pwg.images.addSimple API. Follwing is the used PHP code. After execution form browser, no error is shown. But also nothing happened or printed after ‘Admin login. OK.’ Can someone advise what I am doing wrong?

piwigo-12.2.0
PHP 7.4.16
Ubuntu 21.04)

Code:

<?php
$siteUrl = 'http://www.example.com/';
$url= $siteUrl . "ws.php?format=json";

$user     ='admin';
$password   ='xxxxxxxx';

// set cookie manually from last pwg.session.login call
$cookie = "pwg_id=7o4faju777s9qdifiiucjbub3d";

$postdata = "method=pwg.session.getStatus";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_COOKIE, $cookie); // Here passe the cookie
$resultStatus = curl_exec($ch);
echo "<pre>$resultStatus</pre>";

// connvert to object
$obj = json_decode($resultStatus, TRUE);
$pwg_token = $obj["result"]["pwg_token"];

if ($obj["result"]["username"] == "admin"){
  echo "</br>Admin login. OK.";

  $headers[] = 'Content-Type: multipart/form-data;';

  $filename ='D:\\Temp\\flowers.jpg';
  $postdata = array(
        'method' => 'pwg.images.addSimple',
        'image' => new CurlFile($filename),
        'category' => '1',
        'name' => ' my photo caption ',
        'author' => 'Sir Author',
        'pwg_token'=> $pwg_token
    );


  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_COOKIE, $cookie); // Here passe the cookie
  curl_setopt($ch, CURLOPT_REFERER, $url);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
  
  $resultUpload = curl_exec($ch);
  echo "<pre>$resultUpload</pre>";
  var_dump($resultUpload);
  // Nothing happened here !

} else {
  echo "Error: Not admin login!";
}

curl_close($ch);

?>

Last edited by colorpencil (2022-04-06 15:41:57)

Offline

 
  •  » Extensions
  •  » Unable to upload image using pwg.images.addSimple in PHP

Board footer

Powered by FluxBB

github twitter newsletter Donate Piwigo.org © 2002-2022 · Contact