Hello,
I have a problem with using piwigo's api.
I use the pwg.session.login method but when I do pwg.session.getStatus i am already on the guest user ...
However the pwg.session.login method returns me the following message {"stat":"ok","result":true}
How should I proceed?
Thank you in advance !
Piwigo version: 2.9.4
PHP version: 5.4
MySQL version:
Piwigo URL: http://
Offline
Hi Maxime656_2,
Here is an example in PHP, with script piwigo_remote.php
<?php
if (php_sapi_name() != 'cli')
{
die('this script must be run from cli');
}
$opt = getopt('', array('url:', 'username:', 'password:'));
$mandatory_fields = array('url', 'username', 'password');
foreach ($mandatory_fields as $field)
{
if (!isset($opt[$field]))
{
die('missing --'.$field."\n");
}
}
$cookie_jar = tempnam("/tmp", "COOKIE");
piwigo_login($cookie_jar, $opt['url'], $opt['username'], $opt['password']);
$status = piwigo_get_status($cookie_jar, $opt['url']);
unlink($cookie_jar);
print_r($status);
function piwigo_login($cookie_jar, $piwigo_url, $username, $password)
{
$url = $piwigo_url.'/ws.php?method=pwg.session.login';
$postfields = array(
'username' => $username,
'password' => $password,
);
$postdata = http_build_query($postfields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$resultLogin = curl_exec($ch);
//close connection
curl_close($ch);
}
function piwigo_get_status($cookie_jar, $piwigo_url)
{
$url = $piwigo_url.'/ws.php?method=pwg.session.getStatus&format=json';
$postfields = array(
);
$postdata = http_build_query($postfields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
// set cookie
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
$status = json_decode($result, true)['result'];
return $status;
}
?>and here is how it works:
$ php piwigo_remote.php --url=http://piwigo.org/demo --username=pierrick --password=secretpassword
Array
(
[username] => pierrick
[status] => webmaster
[theme] => modus
[language] => en_UK
[pwg_token] => b51612137zx7608f533epeaf4043977c
[charset] => utf-8
[current_datetime] => 2018-07-24 15:21:09
[version] => 2.9.4
[available_sizes] => Array
(
[0] => square
[1] => thumb
[2] => 2small
[3] => xsmall
[4] => small
[5] => medium
[6] => large
[7] => xlarge
[8] => xxlarge
)
[upload_file_types] => jpg,jpeg,png,gif
[upload_form_chunk_size] => 500
)Offline
Thank you, I figured out how to do it right just before you answered ;)
Offline