Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

irods_-_api_php [2016/12/16 10:15] (Version actuelle)
Ligne 1: Ligne 1:
 +Modifié par Calvat, le 22 Sep 2009\\
 +\\
 +
 +====== IRODS - api php ======
 +
 +\\
 +\\
 +
 +=====  Introduction ​ =====
 +
 +Voici un exemple de l'​utilisation de l'api php d'​iRods. La connection se fait avec un serveur en version 1.1 d'​iRods. Deux bugs ont été corrigé dans l'api pour que cela fonctionne.
 +=====  Installation ​ =====
 +
 +Le package a été téléchargé sur le site web d' [[https://​www.irods.org/​index.php/​Main_Page|iRods]] , rubrique [[https://​www.irods.org/​prods_doc/​|'​PRODS-PHP'​]] de '​client api'. A partir de ce lien se trouve également la documentation.
 +=====  Correction de bugs dans l'api php  =====
 +
 +Deux classes ont été modifiées:
 +
 +  *La méthode getAllChildren de la classe ProdsDir (fichier **ProdsDir.class.php** ) à la ligne 133 et 135: remplacement de getChildrenFiles par getChildFiles et getChildrenDirs par getChildDirs
 +  *Ajout dans le fichier **RODSConn.class.php** à la ligne 631:
 +<​code>​
 +"​COL_D_OWNER_ZONE",​
 +"​COL_D_COMMENTS",​
 +</​code>​
 +Et à la ligne 646:
 +<​code>​
 +$que_result['​COL_D_OWNER_ZONE'​][0],​
 +</​code>​
 +Et à la ligne 649:
 +<​code>​
 +$que_result['​COL_D_COMMENTS'​][0] );
 +</​code>​
 +
 +=====  Exemple de code source utilisant l'​api ​ =====
 +
 +Il faut au préalable disposer des paramètres de connexion à un serveur iRods. Par la suite, ces paramètres sont:
 + ​monServeur
 + ​monPort
 + ​monCompte
 + ​monPwd
 + ​maZone
 + ​monHomeDir
 +
 +qu'il faut remplacer par vos valeurs. Voici alors le contenu d'un fichier php:
 +<​code>​
 +<​html>​
 +<​head>​
 + <​title>​Test api php irods</​title>​
 + <​meta http-equiv=Content-type content="​text/​html;​ charset=iso-8859-15">​
 +</​head>​
 +<​body>​
 +<?​php ​
 +require_once( "​extrods-1.1.0.1beta/​clients/​prods/​src/​Prods.inc.php"​ );
 +
 +#-- make an iRODS account object for connection
 +
 +$account ​ = new RODSAccount(monServeur,​monPort,​monCompte,​monPwd,​maZone);​
 +$home_dir = '​monHomeDir';​
 +
 +#-- create an dir object, assuming the path is $home_dir
 +
 +$home = new ProdsDir( $account, $home_dir );
 +
 +#-- Test if home exists
 +
 +if ( ! $home->​exists() ) {
 + ​print( "​Problem with $home_dir."​ );
 + exit;
 +}
 +
 +#-- Get info on $home_dir
 +
 +$tmpName = $home->​getName();​
 +print( "User: $tmpName"​ );
 +$tmpName = $home->​getPath();​
 +print( "Path: $tmpName."​ );
 +$stat = $home->​getStats();​
 +$tmpName = $stat->​name;​
 +print( "Name = $tmpName"​ );
 +$tmpName = $stat->​ownerzone;​
 +print( "Zone = $tmpName"​ );
 +$tmpName = $stat->​owner;​
 +print( "Owner = $tmpName"​ );
 +
 +#-- list home directory
 +
 +$children = $home->​getAllChildren();​
 +#print_r( $children );
 +
 +#-- print each child'​s name
 +
 +print( "​Children are:" );
 +foreach ( $children as $child ) {
 + ​$tmpName = $child->​getName();​
 + ​print( "​$tmpName"​ ); 
 +}
 +
 +#-- Get child dir (only one level)
 +
 +$tmp = $home->​getChildDirs();​
 +print( "Child dir are:" );
 +foreach ( $tmp as $child ) {
 + ​$tmpName = $child->​getName();​
 + ​print( "​$tmpName"​ );
 + 
 + $stat = $child->​getStats();​
 + ​$tmpName = $stat->​name;​
 + ​print( "name: $tmpName"​ ); 
 + ​$tmpName = $stat->​size;​
 + ​print( "size: $tmpName bytes" ); 
 + ​$tmpName = $stat->​typename;​
 + ​print( "type: $tmpName"​ ); 
 + ​$tmpName = $stat->​id;​
 + ​print( "id: $tmpName"​ ); 
 +}
 +
 +#-- Get child file (on current dir)
 +
 +$tmp = $home->​getChildFiles();​
 +print( "Child file are:" );
 +foreach ( $tmp as $child1 ) {
 + ​$tmpName = $child1->​getName();​
 + ​print( "​$tmpName"​ );
 +
 + $stat = $child1->​getStats();​
 + ​$tmpName = $stat->​name;​
 + ​print( "name: $tmpName"​ ); 
 + ​$tmpName = $stat->​size;​
 + ​print( "size: $tmpName bytes" ); 
 + ​$tmpName = $stat->​typename;​
 + ​print( "type: $tmpName"​ ); 
 + ​$tmpName = $stat->​id;​
 + ​print( "id: $tmpName"​ ); 
 +}
 +
 +#-- Make dir
 +
 +print( "Make dir:" );
 +$mydir ​  = "​titi";​
 +$fulldir = $home_dir . "/"​ . $mydir;
 +$tmp     = new ProdsDir( $account, $fulldir );
 +print( "Make $mydir dir" );
 +if ( $tmp->​exists() ) {
 + ​$tmpPath = $tmp->​getPath();​
 + ​print( "​$tmpPath exists."​ );
 +} else {
 + ​$tmp1 ​   = $home->​mkdir( $mydir );
 + ​$tmpName = $tmp1->​getPath();​
 + ​print( "Path: $tmpName."​ ); 
 +}
 +
 +$mydir ​  = "​titi/​toto";​
 +$fulldir = $home_dir . "/"​ . $mydir;
 +$tmp     = new ProdsDir( $account, $fulldir );
 +print( "Make $mydir dir" );
 +if ( $tmp->​exists() ) {
 + ​$tmpPath = $tmp->​getPath();​
 + ​print( "​$tmpPath exists."​ );
 +} else {
 + $tmp1 = $home->​mkdir( $mydir );
 + ​$tmpName = $tmp1->​getPath();​
 + ​print( "Path: $tmpName."​ ); 
 +}
 +
 +#-- Find file and dirs
 +
 +$myfile['​name'​] = "​OpenLDAP";​
 +print( "Find file with name content: $myfile[name]"​ );
 +$tmp = $home->​findFiles( $myfile, $total_count );
 +foreach ( $tmp as $child ) {
 + ​$tmpPath = $child->​getPath();​
 + ​print( "​$tmpPath"​ );
 +}
 +
 +$myfile['​name'​] = "​to";​
 +print( "Find dirs with name content: $myfile[name]"​ );
 +$tmp = $home->​findDirs( $myfile, $total_count );
 +foreach ( $tmp as $child ) {
 + ​$tmpPath = $child->​getPath();​
 + ​print( "​$tmpPath"​ );
 +}
 +
 +#-- Get meta for the file
 +
 +$myfile ​  = $home_dir . "/​OpenLDAP-Admin-Guide.pdf";​
 +$file     = new ProdsFile( $account, $myfile );
 +$metalist = $file->​getMeta();​
 +print( "Get meta for $myfile:"​ );
 +foreach ( $metalist as $meta ) {
 + ​$id ​ = $meta->​id;​
 + ​print( "id: $id." );
 + ​$tmp ​ = $meta->​name;​
 + ​print( "name: $tmp." );
 + ​$tmp ​ = $meta->​op;​
 + ​print( "op: $tmp." );
 + ​$tmp ​ = $meta->​units;​
 + ​print( ​ "​units:​ $tmp." );
 + ​$tmp ​ = $meta->​value;​
 + ​print( "​value:​ $tmp." );
 +
 +#-- Remove meta by id
 +
 + ​print( "​Remove meta by id for $myfile:"​ );
 + ​$file->​rmMetaByID( $id );
 +}
 +
 +#-- Add meta for the file
 +
 +$metalist = new RODSMeta( "​test",​ 123, "​unit"​ );
 +print( "Set meta for $myfile:"​ );
 +$file->​addMeta( $metalist );
 +
 +#-- Get meta for the file
 +
 +$metalist = $file->​getMeta();​
 +print( "Get meta for $myfile:"​ );
 +foreach ( $metalist as $meta ) {
 + ​$id ​ = $meta->​id;​
 + ​print( "id: $id." );
 + ​$tmp ​ = $meta->​name;​
 + ​print( "name: $tmp." );
 + ​$tmp ​ = $meta->​op;​
 + ​print( "op: $tmp." );
 + ​$tmp ​ = $meta->​units;​
 + ​print( "​units:​ $tmp." );
 + ​$tmp ​ = $meta->​value;​
 + ​print( "​value:​ $tmp." );
 +
 +#-- Remmove meta by meta object
 +
 + ​print( "​Remove meta for $myfile:"​ );
 + ​$file->​rmMeta( $meta );
 +}
 +
 +#-- Add meta for the file
 +
 +$metalist = new RODSMeta( "​test2",​ 456, "​unit"​ );
 +print( "Set meta for $myfile:"​ );
 +
 +#-- Get meta for the file
 +
 +$file->​addMeta( $metalist );
 +$metalist = $file->​getMeta();​
 +print( "Get meta for $myfile:"​ );
 +foreach ( $metalist as $meta ) {
 + ​$id ​ = $meta->​id;​
 + ​print( "id: $id." );
 + ​$tmp ​ = $meta->​name;​
 + ​print( "name: $tmp." );
 + ​$tmp ​ = $meta->​op;​
 + ​print( "op: $tmp." );
 + ​$tmp ​ = $meta->​units;​
 + ​print( "​units:​ $tmp." );
 + ​$tmp ​ = $meta->​value;​
 + ​print( "​value:​ $tmp." );
 +
 +#-- Remove meta by id
 +
 + ​print( "​Remove meta by id for $myfile:"​ );
 + ​$file->​rmMetaByID( $id );
 +}
 +
 +#-- Remove dir
 +
 +print( "​Remove dir:" );
 +$mydir ​  = "​titi";​
 +$fulldir = $home_dir . "/"​ . $mydir;
 +$tmp     = new ProdsDir( $account, $fulldir );
 +$tmpPath = $tmp->​getPath();​
 +print( "​Remove $tmpPath dir" );
 +if ( $tmp->​exists() ) {
 + ​$tmp->​rmdir( TRUE, TRUE );
 + ​print( "​$tmpPath removed."​ );
 +} else {
 + ​print( "​$tmpPath does not exist."​ ); 
 +}
 +
 +#-- Download a file
 +
 +$myfile ​  = $home_dir . "/​OpenLDAP-Admin-Guide.pdf";​
 +$file     = new ProdsFile( $account, $myfile );
 +print( "Open $myfile in read mode" );
 +if ( $file->​exists() ) {
 + ​print( "<​iframe id=file_download width=0 height=0 scrolling=no frameborder=0 src=read.php?​myfile=$myfile></​iframe>"​ );
 +} else {
 + ​print( "​$myfile does not exist."​ );
 +}
 +
 +#-- Put an image on the page
 +
 +$myfile ​  = "​mon_image_dans_irods";​
 +$file     = new ProdsFile( $account, $myfile );
 +$stat     = $file->​getStats();​
 +$size     = 0;
 +$size     = $stat->​size;​
 +$size     = number_format( $size, '',​ ',',​ ' ' );
 +print( "Open $myfile in read mode ($size bytes)"​ );
 +if ( $file->​exists() ) {
 + ​print( ""​ );
 +} else {
 + ​print( "​$myfile does not exist."​ );
 +}
 +
 +exit;
 +?>
 +</​body>​
 +</​html>​
 +</​code>​
 +Pour pouvoir télécharger le fichier dans iRods à partir de cette page web, il faut créer le fichier php **read.php** dans la même arborescence que le fichier précédant et contenant:
 +<​code>​
 +<?php
 +ob_start();
 +require_once( "​extrods-1.1.0.1beta/​clients/​prods/​src/​Prods.inc.php"​ );
 +
 +$myfile = $_REQUEST['​myfile'​];​
 +if ( ! $myfile ) { exit; }
 +
 +$filename = basename( $myfile );
 +$account ​ = new RODSAccount(monServeur,​monPort,​monCompte,​monPwd,​maZone);​
 +$file     = new ProdsFile( $account, $myfile );
 +$stat     = $file->​getStats();​
 +$size     = $stat->​size;​
 +
 +header( "​Content-Description:​ File Transfer"​ );             
 +header( "​Content-Disposition:​ attachment; filename=$filename"​ );
 +header( "​Content-Type:​ application/​octet-stream"​ );
 +header( "​Content-Type:​ application/​force-download"​ );
 +header( "​Content-Type:​ application/​download"​ );
 +header( "​Content-Transfer-Encoding:​ binary"​ );
 +header( "​Content-Length:​ $size" );
 +header( "​Cache-Control:​ no-cache, must-revalidate"​ );
 +
 +$file->​open( "​r",​ NULL, FALSE );
 +$seek = $file->​seek( 0 );
 +ob_end_clean();​
 +while( $str = $file->​read(4096) ) { echo $str; }
 +$file->​close();​
 +exit;
 +?>
 +</​code>​
 +Pour pouvoir inclure une image d'​iRods dans cette page web, il faut créer le fichier php **read1.php** dans la même arborescence que le fichier précédant et contenant:
 +<​code>​
 +<?php
 +ob_start();
 +require_once( "​extrods-1.1.0.1beta/​clients/​prods/​src/​Prods.inc.php"​ );
 +
 +$myfile = $_REQUEST['​myfile'​];​
 +if ( ! $myfile ) { exit; }
 +
 +$filename = basename( $myfile );
 +$account ​ = new RODSAccount(monServeur,​monPort,​monCompte,​monPwd,​maZone);​
 +$file     = new ProdsFile( $account, $myfile );
 +$stat     = $file->​getStats();​
 +$size     = $stat->​size;​
 +
 +header( "​Content-Type:​ image/​tiff"​ );
 +header( "​Content-Length:​ $size" );
 +header( "​Cache-Control:​ no-cache, must-revalidate"​ );
 +
 +$file->​open( "​r",​ NULL, FALSE );
 +$seek = $file->​seek( 0 );
 +ob_end_clean();​
 +while( $str = $file->​read(4096) ) { echo $str; }
 +$file->​close();​
 +exit;
 +?>
 +</​code>​
 +
  
  • irods_-_api_php.txt
  • Dernière modification: 2016/12/16 10:15
  • (modification externe)