Documentation

« Retour à la librairie Net_I2C

  1. Documentation de Net_I2C
  2. Code source de Net_I2C


Documentation de Net_I2C

Description

Cette classe est une interface entre le site utilisateur et les services i2c. Elle permet d’obtenir le code ISO (deux lettres) du pays de l’adresse IP (v4 ou v6) fournie.

Deux modes de connexion disponibles :

  • SOAP (par défaut)
  • HTTP (requête GET)

Ainsi, après la création simple de l’instance Net_I2C, vous pouvez obtenir le pays d’origine de n’importe quelle adresse IP (v4 comme v6) de cette manière: <?php $i2c = new Net_I2C(‘soap’); // ou ‘http’ $country = $i2c->getCountry(’12.34.56.78′); var_dump($country); ?> Affiche: string(2) "us"


Résumé des variables
public string $methode
private boolean $_connected
private array $_methodes

Résumé des méthodes
public Net_I2C __construct([ string   $methode = 'soap' ])
public string(2) getCountry( string   $adresse_ip )
private boolean _connectWithHTTP()
private boolean _connectWithSOAP()
private void _connexion()
private string _getCountryWithHTTP( string   $adresse_ip )
private string _getCountryWithSOAP( string   $adresse_ip )

Variables
public string $methode (line 63 )

Méthode de connexion choisie pour cette instance de Net_I2C

  • var: Méthode de connexion
  • access: public

private boolean $_connected = false (line 56 )

Détermine si l’on est connecté à l’API ou non…

  • var: True == Connecté, False == Non Connecté
  • access: private

private array $_methodes = array(‘soap’ =>… (line 38 )

Contient la liste des méthodes de connexion autorisées comme clé et comme valeur un tableau associatif de la fonction à utiliser pour la récupération, la fonction à utiliser pour la connexion ainsi que de l’URL du service.

  • var: Méthodes de connexion disponibles
  • access: private

Méthodes
Constructeur __construct (line 74)

Constructeur de Net_I2C.

  • throws: Exception quand la méthode est invalide
  • access: public
Net_I2C__construct([string $methode = 'soap'])
  • string$methode: Méthode de connexion

public getCountry (line 91)

Retourne le pays en fonction de l’adresse IP fournie.

  • return: Pays de l’IP
  • throws: Exception si la fonction de récupération n’éxiste pas.
  • access: public
string(2)getCountry(string $adresse_ip)
  • string$adresse_ip: Adresse IP v4 ou v6

private _connectWithHTTP (line 193)

Connexion via HTTP.

En réalité, cette fonction ne fait rien puisqu’il ne faut pas se connecter avant pour utiliser HTTP.

  • return: = true
  • access: private
boolean_connectWithHTTP()

private _connectWithSOAP (line 173)

Connexion à l’API i2c avec SOAP

  • return: = true
  • throws: Exception si la connexion à échoué.
  • access: private
boolean_connectWithSOAP()

private _connexion (line 154)

Appelle la fonction de connexion spécifique à la méthode utilisée.

  • throws: Exception si la fonction de connexion définie par la méthode n’éxiste pas
  • access: private
void_connexion()

private _getCountryWithHTTP (line 131)

Récupèrer le pays de l’IP en HTTP

  • return: Pays de l’IP
  • access: private
string_getCountryWithHTTP(string $adresse_ip)
  • string$adresse_ip: Adresse IP

private _getCountryWithSOAP (line 114)

Récupèrer le pays en utilisant SOAP

  • return: Pays de l’IP
  • access: private
string_getCountryWithSOAP(string $adresse_ip)
  • string$adresse_ip: Adresse IP


Code source de Net_I2C

  1. <?php
  2. /**
  3.  * Cette classe est une interface entre le site utilisateur et les services i2c. Elle
  4.  * permet d’obtenir le code ISO (deux lettres) du pays de l’adresse IP (v4 ou v6) fournie.
  5.  * Deux modes de connexion disponibles :
  6.  *     - SOAP (par défaut)
  7.  *     - HTTP (requête GET)
  8.  * 
  9.  * Ainsi, après la création simple de l’instance Net_I2C, vous pouvez obtenir le pays
  10.  * d’origine de n’importe quelle adresse IP (v4 comme v6) de cette manière:
  11.  * <?php
  12.  * $i2c = new Net_I2C(‘soap’); // ou ’http’
  13.  * $country = $i2c->getCountry(’12.34.56.78′);
  14.  * var_dump($country);
  15.  * ?>
  16.  * Affiche: string(2) "us"
  17.  * 
  18.  * @category Net
  19.  * @package Net_I2C
  20.  * @author Samuel ROZE <samuel.roze@d-sites.com>
  21.  * @copyright 2009 Samuel ROZE
  22.  * @link http://www.d-sites.com/projets/i2c/
  23.  * @link http://www.d-sites.com/librairies/Net_I2C/
  24.  * @link http://pear.php.net/package/Net_I2C
  25.  *
  26.  */
  27. class Net_I2C 
  28. {
  29.     // {{{ properties
  30.     /**
  31.      * Contient la liste des méthodes de connexion autorisées comme clé
  32.      * et comme valeur un tableau associatif de la fonction à utiliser
  33.      * pour la récupération, la fonction à utiliser pour la connexion
  34.      * ainsi que de l’URL du service.
  35.      * 
  36.      * @var array Méthodes de connexion disponibles
  37.      */
  38.     private $_methodes = array(
  39.         ‘soap’ => array(
  40.             ‘func_connect’ => ‘_connectWithSOAP’,
  41.             ‘func_get’ => ‘_getCountryWithSOAP’,
  42.             ‘url’ => ‘http://i2c.mes-stats.fr/i2c.wsdl’
  43.         ),
  44.         ‘http’ => array(
  45.             ‘func_connect’ => ‘_connectWithHTTP’,
  46.             ‘func_get’ => ‘_getCountryWithHTTP’,
  47.             ‘url’ => ‘http://i2c.mes-stats.fr/get?ip=’
  48.         )
  49.     );
  50.     
  51.     /**
  52.      * Détermine si l’on est connecté à l’API ou non…
  53.      * 
  54.      * @var boolean True == Connecté, False == Non Connecté
  55.      */
  56.     private $_connected = false;
  57.     
  58.     /**
  59.      * Méthode de connexion choisie pour cette instance de Net_I2C
  60.      * 
  61.      * @var string Méthode de connexion
  62.      */
  63.     public $methode;
  64.     
  65.     // }}}
  66.     // {{{ __construct()
  67.     /**
  68.      * Constructeur de Net_I2C.
  69.      * 
  70.      * @param string $methode Méthode de connexion
  71.      * @return Net_I2C 
  72.      * @throws Exception quand la méthode est invalide
  73.      */
  74.     public function __construct($methode ‘soap’
  75.     {
  76.         if (!array_key_exists($methode$this->_methodes)) {
  77.             throw new Exception(‘La méthode est invalide’E_USER_ERROR);
  78.         }
  79.         $this->methode = $methode;
  80.     }
  81.     
  82.     // }}}
  83.     // {{{ getCountry()
  84.     /**
  85.      * Retourne le pays en fonction de l’adresse IP fournie.
  86.      * 
  87.      * @param string $adresse_ip Adresse IP v4 ou v6
  88.      * @return string(2) $country Pays de l’IP
  89.      * @throws Exception si la fonction de récupération n’éxiste pas.
  90.      */
  91.     public function getCountry ($adresse_ip)
  92.     {
  93.         if (!$this->_connected{
  94.             $this->_connexion();
  95.         }
  96.         
  97.         $function $this->_methodes[$this->methode]['func_get'];
  98.         
  99.         if (!($country $this->$function($adresse_ip))) {
  100.             throw new Exception(‘La fonction de récupération n’éxiste pas’E_USER_ERROR);
  101.         }
  102.         
  103.         return $country;
  104.     }
  105.     
  106.     // }}}
  107.     // {{{ _getCountryWithSOAP()
  108.     /**
  109.      * Récupèrer le pays en utilisant SOAP
  110.      * 
  111.      * @param string $adresse_ip Adresse IP
  112.      * @return string $country Pays de l’IP
  113.      */
  114.     private function _getCountryWithSOAP ($adresse_ip{
  115.         try {
  116.             $country $this->_methodes['soap']['client']->getCountry($adresse_ip);
  117.         catch (SoapFault $exception{
  118.             throw new Exception(‘SOAP Exception: ’.$exceptionE_USER_ERROR);      
  119.         }
  120.         return $country;
  121.     }
  122.     
  123.     // }}}
  124.     // {{{ _getCountryWithHTTP()
  125.     /**
  126.      * Récupèrer le pays de l’IP en HTTP
  127.      * 
  128.      * @param string $adresse_ip Adresse IP
  129.      * @return string $country Pays de l’IP
  130.      */
  131.     private function _getCountryWithHTTP ($adresse_ip{
  132.         $fp fopen ($this->_methodes['http']['url'].$adresse_ip‘r’);
  133.         if (!$fp{
  134.             throw new Exception(‘Impossible d’ouvrir la connexion’E_USER_ERROR)
  135.         
  136.         else {
  137.             $country fgets ($fp);
  138.             if (strlen($country!= 2{
  139.                 throw new Exception(fgets ($fp)E_USER_ERROR);
  140.             }
  141.         
  142.         fclose($fp)
  143.         return $country;
  144.     }
  145.     
  146.     // }}}
  147.     // {{{ _connexion()
  148.     /**
  149.      * Appelle la fonction de connexion spécifique à la méthode utilisée.
  150.      * 
  151.      * @return void 
  152.      * @throws Exception si la fonction de connexion définie par la méthode n’éxiste pas
  153.      */
  154.     private function _connexion ()
  155.     {
  156.         $function $this->_methodes[$this->methode]['func_connect'];
  157.         
  158.         if (!$this->$function()) {
  159.             throw new Exception(‘La fonction de connexion n’éxiste pas’E_USER_ERROR);
  160.         }
  161.         
  162.         $this->_connected = true;
  163.     }
  164.     
  165.     /// }}}
  166.     /// {{{ _connectWithSOAP()
  167.     /**
  168.      * Connexion à l’API i2c avec SOAP
  169.      * 
  170.      * @return boolean = true
  171.      * @throws Exception si la connexion à échoué.
  172.      */
  173.     private function _connectWithSOAP ()
  174.     {
  175.         try {
  176.             $this->_methodes['soap']['client'new SoapClient($this->_methodes['soap']['url']);
  177.         catch (SoapFault $exception{
  178.             throw new Exception(‘SOAP Exception: ’.$exceptionE_USER_ERROR);      
  179.         }
  180.         
  181.         return true;
  182.     }
  183.     
  184.     /// }}}
  185.     /// {{{ _connectWithHTTP()
  186.     /**
  187.      * Connexion via HTTP.
  188.      * En réalité, cette fonction ne fait rien puisqu’il ne faut pas se connecter
  189.      * avant pour utiliser HTTP.
  190.      * 
  191.      * @return boolean = true
  192.      */
  193.     private function _connectWithHTTP ()
  194.     {
  195.         return true;
  196.     }
  197.     
  198.     /// }}}
  199. }

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Notify me of followup comments via e-mail. You can also subscribe without commenting.