Announcement

  •  » Engine
  •  » [resolved] PHP API Login

#1 2013-04-06 18:38:35

Polly
Translation Team
2010-01-15
60

[resolved] PHP API Login

Hey, im trying to login to the API and pull images and desciptions into TYPO3 frontend.

it kinda works for public categories and images.
i get positive response from login in PHP

{"stat":"ok","result":true}

http://dev.sam-net.de/index.php?id=1

How can i login to the API using PHP and pulling images und categories ?

My script:

Code:

<?php

require_once(PATH_tslib.'class.tslib_pibase.php');

class user_piwigoConnect {

  var $cObj; // BACK REFS UPPER cObj

  /**

   * CALL FUNCTION

   */

  function main($content,$conf){
    
    // LOGIN TO PIWIGO
    $url = $conf['url'] . 'ws.php?format=json';

    $postData = array();
    $postData['method'] = 'pwg.session.login';
    $postData['username'] = $conf['user'];
    $postData['password'] = $conf['password'];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    $resultLogin = curl_exec($ch);
    curl_close($ch);

    // GET URL PARAMS
    $get_vars = t3lib_div::_GET();

    // INCLUDE DYNAMIC JS
    $script .= "
    <script type='text/javascript'>
    </script>
    " . print_r($postData);
    

    $getCat = file_get_contents("http://neu.lacordee-bildarchiv.de/ws.php?format=json&method=pwg.categories.getList");

      $catsJson = json_decode($getCat, true);
      $categories = '';
      foreach ($catsJson['result']['categories'] as $item) {
        
          $id = $item['id'];
          $catName = $item['name'];
          
          
          if($item['comment'] == null) {
          $comment = '';
          } else {
            $comment = '<br>' . $item['comment'];
          }
        
         $categories .= '<br>' . $id . ' ######## ' . $catName;
      }          

    $getImages = file_get_contents("http://neu.lacordee-bildarchiv.de/ws.php?format=json&method=pwg.categories.getImages&recursive=false&cat_id=84");
    
    $login = json_decode($resultLogin);
    
    $content .= $resultLogin;
    
    if($login->{'stat'} == 'fail') {
      $loginMsg = 'Login Fehler, prüfen Sie die Zugangsdaten!';
    } else if($login->{'stat'} == 'ok') {
      $imagesJson = json_decode($getImages, true);
      
      $images = $login->{'stat'} . $conf['catDefault'] . '';
      foreach ($imagesJson['result']['images']['_content'] as $item) {
        
          $image = $item['derivatives']['square']['url'];
          $width = $item['derivatives']['square']['width'];
          $height = $item['derivatives']['square']['height'];
          $imageHd = $item['derivatives']['xlarge']['url'];
          $imageHdWidth = $item['derivatives']['xlarge']['width'] + 20;
          $imageHdHeight = $item['derivatives']['xlarge']['height'] + 20;
          
          
          if($item['comment'] == null) {
          $comment = '';
          } else {
            $comment = '<br>' . $item['comment'];
          }
        
         $images .= '<div class="image" style="width: ' . $width . 'px">';
         $images .= '<a title="<b>' . $item['name'] . '</b>' . $comment . '" href="' . $imageHd . '" rel="shadowbox[piwigo];width=' . $imageHdWidth . ';height=' . $imageHdHeight . '"><img src="' . $image . '" width="' . $width . '" height="' . $height . '"/></a>';
         $images .= '<div class="caption">' . $item['name'] . '</div>';
         $images .= '</div>';
      }          

    }
    $content.= $script . $get_vars['piwigoCat'] . $loginMsg . '<div id="piwigo">' .  $images . '</div>' . $categories . $getCat;
    return $content;
  }

}

?>

Thanks,
polly

Offline

 

#2 2013-04-11 09:24:00

Polly
Translation Team
2010-01-15
60

Re: [resolved] PHP API Login

Hey....

this does the Job:

Code:

    // SET $CONF AS VARIABLE
    $user = $conf['user'];
    $password = $conf['password'];
    $siteUrl = $conf['url'];
    $catDefault = $conf['catDefault'];
    $browserCatCount = $conf['catCount'];
    $browserImageCount = $conf['imageCount'];
    $browserCatCount = $conf['catCount'];

    // LOGIN TO PIWIGO
    $url= $siteUrl . "ws.php?format=json";
    $postdata = "method=pwg.session.login&username=" . $user . "&password=" . $password;

    $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, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
    curl_setopt($ch, CURLOPT_POST, 1);
    $resultLogin = curl_exec($ch);

    preg_match_all('|Set-Cookie: (.*);|U', $resultLogin, $results);
    $cookies = $results[1][2];

Offline

 
  •  » Engine
  •  » [resolved] PHP API Login

Board footer

Powered by FluxBB

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