Hi,
I am developing a PHP class that queries a Piwigo instance via the API. This is working perfectly for all but one option - one which requires an admin user to be logged in.
A section of the code from my class is listed below. The call to "pwg.session.login" returns
{"stat":"ok","result":true}
However the subsequent call to "pwg.session.getStatus" then returns
{"stat":"ok","result":{"username":"guest","status":"guest","theme":"hamclassics","language":"en_UK","pwg_token":"40dd2015ff8da963298e18f944fee5cf","charset":"utf-8","current_datetime":"2016-10-15 02:18:33","version":"2.8.2"}}
So I am unable to obtain the token string, and my admin call later in the process fails as such.
What can I do to make this work?
public function __construct() { $this->Context = stream_context_create(); } public function Login() { if ($this->Settings->Username == "") { return ""; } $dictQuery = [ "username" => $this->Settings->Username, "password" => $this->Settings->Password ]; $oResponse = $this->PostWebServiceRequest("pwg.session.login", $dictQuery); if ($oResponse->result !== true) { SetMessage("Login failure", false); return false; } $oResponse = $this->PostWebServiceRequest("pwg.session.getStatus"); if ($oResponse->result->username != $this->Settings->Username) { SetMessage("Login failure - unable to obtain token", false); return false; } // All OK $this->Key = $oResponse->result->pwg_token; $this->IsLoggedIn = true; return true; } public function PostWebServiceRequest($sMethod, $dictQuery = array(), $bDebug = false) { $sPostURL = $this->Settings->PiwigoURL . "ws.php?format=json"; $dictQuery["method"] = $sMethod; $oPostData = http_build_query($dictQuery); $oHTTPOpts = array('http' => array( 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $oPostData ) ); stream_context_set_option($this->Context, $oHTTPOpts); $sResult = file_get_contents($sPostURL, false, $this->Context); $oObject = json_decode($sResult); if ($bDebug) { // We need to debug this? OK - Dump everything... Dump($sPostURL, $dictQuery, $sResult, $oObject); } return $oObject; }
Piwigo version: Latest
PHP version: 5.5
MySQL version:
Piwigo URL: Internal
Thanks,
Mark
Offline
You need to get the cookies sent with the response to your login request and send it in subsequent requests
Offline
OK, thanks for that - any advice on how to do this in PHP?
Mark
Offline
Figured it out!!!
Thank you so much!
Mark
Offline
tip2tail wrote:
Figured it out!!!
please post your piece of code to do that, I'm 100% sure other people will find it useful, including me ;-)
Offline