Announcement

#1 2018-11-29 07:44:50

justice41
Member
2018-11-19
23

Piwigo api login

Hello/Hi/Greetings,

I'm trying to use piwigo's api login to enable users to login in their piwigo account if they login on my front site.. i'm getting a "string(27) "{"stat":"ok","result":true}"  response.. however the user is still not logged in... am i getting something wrong??


................ a snipper of a php file i'm working on...

$url = "http://127.0.0.1/gallery/ws.php?format=json&method=pwg.session.login";
# Our new data
$data = array(
  'username' => "testuser01",
  'password' => 'testuser01',
);
function curl_post($url, $post,array $options = array())
{
  $defaults = array(
    CURLOPT_POST => 1,
    CURLOPT_HEADER => 0,
    CURLOPT_URL => $url,
    CURLOPT_FRESH_CONNECT => 1,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_FORBID_REUSE => 1,
    CURLOPT_TIMEOUT => 4,
    CURLOPT_POSTFIELDS => http_build_query($data)
  );

  $ch = curl_init();
  curl_setopt_array($ch, ($options + $defaults));
  if( ! $result = curl_exec($ch))
  {
    trigger_error(curl_error($ch));
  }
  curl_close($ch);
  return $result;
}
$res = curl_post($url,$data);
var_dump($res);

any help is appreciated

Last edited by justice41 (2018-11-29 07:46:09)

Offline

 

#2 2018-12-05 11:11:28

Nati
Member
2018-11-29
4

Re: Piwigo api login

Hey, I had the same problem and i just solved it.  Here my solution:

// LOGIN
$WSurl = 'https://casmorges.piwigo.com/ws.php?format=json';
$WSdata = "method=pwg.session.login&username=" . $user . "&password=" . $password;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $WSurl);
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, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_REFERER, $WSurl);
curl_setopt($ch, CURLOPT_POSTFIELDS, $WSdata);
curl_setopt($ch, CURLOPT_POST, 1);
$resultLogin = curl_exec($ch);

//GET COOKIE
preg_match_all('|Set-Cookie: (.*);|U', $resultLogin, $results);
$cookie = $results[1][1];
           
curl_close($ch);

// GET STATUS
$WSurl = 'https://casmorges.piwigo.com/ws.php?format=json';
$WSdata = "method=pwg.session.getStatus";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $WSurl);
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, 1);
curl_setopt($ch, CURLOPT_COOKIE, $cookie); // Here passe the cookie
curl_setopt($ch, CURLOPT_REFERER, $WSurl);
curl_setopt($ch, CURLOPT_POSTFIELDS, $WSdata);
$resultLogin = curl_exec($ch);

curl_close($ch);

Offline

 

Board footer

Powered by FluxBB

github twitter facebook google+ newsletter Donate Piwigo.org © 2002-2018 · Contact