IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Zend Framework PHP Discussion :

Mettre un flux XML avec Zend_Cache et Zend_Http_Client [ZF 1.11]


Sujet :

Zend Framework PHP

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Homme Profil pro
    Ingénieur d'études en développements techniques
    Inscrit en
    Novembre 2005
    Messages
    244
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur d'études en développements techniques
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Novembre 2005
    Messages : 244
    Par défaut Mettre un flux XML avec Zend_Cache et Zend_Http_Client
    Bonjour,

    Je suis en train de développer une classe sous ZF qui me récupère un flux XML et la met en cache, j'utilise des objets ZF pour ça, mais je rencontre plusieurs difficultés, on va commencer par la première, je dois nommer le fichier en m'appuyant sur la date du jour, mais j'ai une erreur
    Message: Invalid id or tag 'xmlComment_cache_2011-35-30_fastbooking' : must use only [a-zA-Z0-9_]
    Voici mon code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
     
    <?php
    class App_Fastbooking
    {
    	/**
    	 * Username
    	 *
    	 * @var string
    	 */
    	static protected $_strUsername = 'XXXX';
     
    	/**
    	 * Password
    	 *
    	 * @var string
    	 */
    	static protected $_strPassword = 'zzzzzzz';
     
    	/**
    	 * Service url
    	 *
    	 * @var string
    	 */
    	static protected $_strServiceUrl = 'http://apixml.fastbooking.net/XMLcomments.php';
     
    	/**
    	 * Start date of request
    	 *
    	 * @var string
    	 */
    	static protected $_strStartDate;
     
    	/**
    	 * End date of request
    	 *
    	 * @var string
    	 */
    	static protected $_strEndDate;
     
    	/**
    	 * Current service instance
    	 *
    	 * @var Hgp_Lib_Service_Fastbooking
    	 */
    	static protected $_instance;
     
    	/**
    	 * Nb Days on the demand period
    	 *
    	 * @var Integer
    	 */
    	static protected $_intNbDays = 30;
     
    	/**
    	 * Defautl Cache life in hour
    	 *
    	 * @var Integer
    	 */
    	static protected $_intCacheLife = 24;
     
    	/**
    	 * xmlContent
    	 *
    	 * @var string
    	 */
    	static protected $_xmlContent;
     
    	/**
    	 * $_client
    	 *
    	 * @object Zend_Http_Client
    	 */
    	static protected $_client;
     
    	/**
    	 * Constructor
    	 * Init instance
    	 */
    	public function __construct()
    	{
     
    		static $beginDate;
     
    		if (!isset($beginDate) || is_null($beginDate)) {
    			$beginDate = Zend_Date::now('en_US');
    		}
    		$endDate = clone($beginDate);
    		$endDate->add((int) self::$_intNbDays, Zend_Date::DAY);
     
    		$endDate = clone($beginDate);
    		$endDate->add(self::$_intNbDays, Zend_Date::DAY);
     
    		self::$_strStartDate = $beginDate->toString('Y-m-d');
    		self::$_strEndDate   = $endDate->toString('Y-m-d');
     
    		$strRequest = '<?xml version="1.0" encoding="utf-8" ?>'
        	. '<REQUEST>'
        	. '<HEADER Username="'.self::$_strUsername.'" Password="'.self::$_strPassword.'" Protocol="1.0" />'
        	. '<XMLCOMMENTS startDate="'.self::$_strStartDate.'" endDate="'.self::$_strEndDate.'"/>'
        	. '</REQUEST>';
     
    		self::$_client = new Zend_Http_Client(self::$_strServiceUrl, array( 'adapter' => 'Zend_Http_Client_Adapter_Curl' ));
        	self::$_client->setParameterPost('xdata', $strRequest);
     
    		self::$_instance = $this;
    		//self::$_instance = $client;
    	}
     
    	/**
    	 * Returns instance
    	 *
    	 * @return Hgp_Lib_Service_AvailPro
    	*/
    	static public function getInstance()
    	{
    		if (!self::$_instance instanceof self) {
    			self::$_instance = new self();
    		}
     
    		return self::$_instance;
    	}
     
    	protected function getCache($content)
        {
        	//The current date
        	static $currDate;
    		if (!isset($currDate)) {		
    		    $currDate = Zend_Date::now('en_US')->toString('Y-m-d');		
    		}
     
        	// has cache ?
        	$cacheId = 'xmlComment_cache_'.$currDate.'_fastbooking';
     
    		// Cache location
    		$_cacheComment =  CACHE_DIR.'/';
     
        	$frontendOptions = array( 'automatic_serialization' => true );
    		$backendOptions = array( 'cacheDir' => $_cacheComment );
     
    		$cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
    		//$cache->remove($cacheId);
    		$cacheValue = $cache->load($cacheId);
     
    		if ($cacheValue) {
    			self::$_xmlContent = $cacheValue;
    			return 200;
    		}
     
     
            // set cache
            $cache->setLifetime(3600 * self::$_intCacheLife); 
            $cache->save($content, $cacheId);
     
     
            return self::$_xmlContent;
        }
     
    	/**
    	 * 
    	 *
    	 * @var int $aHotelId
    	 * @return stdClass
    	 */
    	public function getAllComment()
    	{
    		$arrReturn = array();
    		$client = self::getInstance();
    		$xmlResponse = simplexml_load_string(self::$_client->request('POST')->getBody());
    		//$xmlResponse = self::$_client->request('POST')->getBody();
    		$arrReturn = self::getCache($xmlResponse);			
    		return (object) $arrReturn;
    		//return $xmlResponse;
    	}
    }

  2. #2
    Expert confirmé

    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    7 920
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 7 920
    Par défaut
    Y-m-d >> Y_m_d

  3. #3
    Membre éclairé
    Homme Profil pro
    Ingénieur d'études en développements techniques
    Inscrit en
    Novembre 2005
    Messages
    244
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur d'études en développements techniques
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Novembre 2005
    Messages : 244
    Par défaut
    Oui merci, j'étais justement en train de voir mon erreur , j'avais carrément supprimé les "-", mais bon c'est aussi bien de mettre "_", ça reste lisible, merci beaucoup !

  4. #4
    Membre éclairé
    Homme Profil pro
    Ingénieur d'études en développements techniques
    Inscrit en
    Novembre 2005
    Messages
    244
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur d'études en développements techniques
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Novembre 2005
    Messages : 244
    Par défaut Plus de message d'erreur mais le flux est vide
    Je continue sur mon objet ZF,
    voici le code obtenu :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
     
    <?php
    class App_Fastbooking
    {
    	/**
    	 * Username
    	 *
    	 * @var string
    	 */
    	static protected $_strUsername = 'XXXX';
     
    	/**
    	 * Password
    	 *
    	 * @var string
    	 */
    	static protected $_strPassword = 'zzzzzzz';
     
    	/**
    	 * Service url
    	 *
    	 * @var string
    	 */
    	static protected $_strServiceUrl = 'http://apixml.fastbooking.net/XMLcomments.php';
     
    	/**
    	 * Start date of request
    	 *
    	 * @var string
    	 */
    	static protected $_strStartDate;
     
    	/**
    	 * End date of request
    	 *
    	 * @var string
    	 */
    	static protected $_strEndDate;
     
    	/**
    	 * Current service instance
    	 *
    	 * @var Hgp_Lib_Service_Fastbooking
    	 */
    	static protected $_instance;
     
    	/**
    	 * Nb Days on the demand period
    	 *
    	 * @var Integer
    	 */
    	static protected $_intNbDays = 30;
     
    	/**
    	 * Defautl Cache life in hour
    	 *
    	 * @var Integer
    	 */
    	static protected $_intCacheLife = 24;
     
    	/**
    	 * xmlContent
    	 *
    	 * @var string
    	 */
    	static protected $_xmlContent;
     
    	/**
    	 * $_client
    	 *
    	 * @object Zend_Http_Client
    	 */
    	static protected $_client;
     
    	/**
    	 * Constructor
    	 * Init instance
    	 */
    	public function __construct()
    	{
     
    		static $beginDate;
     
    		if (!isset($beginDate) || is_null($beginDate)) {
    			$beginDate = Zend_Date::now('en_US');
    		}
    		$endDate = clone($beginDate);
    		$endDate->add((int) self::$_intNbDays, Zend_Date::DAY);
     
    		$endDate = clone($beginDate);
    		$endDate->add(self::$_intNbDays, Zend_Date::DAY);
     
    		self::$_strStartDate = $beginDate->toString('Y-m-d');
    		self::$_strEndDate   = $endDate->toString('Y-m-d');
     
    		$strRequest = '<?xml version="1.0" encoding="utf-8" ?>'
        	. '<REQUEST>'
        	. '<HEADER Username="'.self::$_strUsername.'" Password="'.self::$_strPassword.'" Protocol="1.0" />'
        	. '<XMLCOMMENTS startDate="'.self::$_strStartDate.'" endDate="'.self::$_strEndDate.'"/>'
        	. '</REQUEST>';
     
    		self::$_client = new Zend_Http_Client(self::$_strServiceUrl, array( 'adapter' => 'Zend_Http_Client_Adapter_Curl' ));
        	self::$_client->setParameterPost('xdata', $strRequest);
     
    		self::$_instance = $this;
    	}
     
    	/**
    	 * Returns instance
    	 *
    	 * @return Hgp_Lib_Service_AvailPro
    	*/
    	static public function getInstance()
    	{
    		if (!self::$_instance instanceof self) {
    			self::$_instance = new self();
    		}
     
    		return self::$_instance;
    	}
     
    	protected function getCache($content)
        {
        	//The current date
        	static $currDate;
    		if (!isset($currDate)) {		
    		    $currDate = Zend_Date::now('en_US')->toString('Ymd');		
    		}
     
        	// has cache ?
        	$cacheId = 'xmlComment_cache_'.$currDate.'_fastbooking';
     
    		// Cache location
    		$_cacheComment =  CACHE_DIR.'/';
     
        	$frontendOptions = array( 'automatic_serialization' => true );
    		$backendOptions = array( 'cacheDir' => $_cacheComment );
     
    		$cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
    		//$cache->remove($cacheId);
    		$cacheValue = $cache->load($cacheId);
     
    		if ($cacheValue) {
    			self::$_xmlContent = $cacheValue;
    			return 200;
    		}
     
     
            // set cache
            $cache->setLifetime(3600 * self::$_intCacheLife); 
            $cache->save($content, $cacheId);
     
     
            return self::$_xmlContent;
        }
     
     
    	/**
    	 * Returns All comments
    	 *
    	 * @return arrReturn
    	 */
    	public function getAllComment()
    	{
    		$arrReturn = array();
    		self::getInstance();
    		//$arrReturn = simplexml_load_string(self::getCache(self::$_client->request('POST')->getBody()));			
    		$arrReturn = self::getCache(self::$_client->request('POST')->getBody());			
    		return (object) $arrReturn;
    	}
    }
    voici mon lien du Zend_Debug::dump Mais le flux est vide, ais-je mal fait quelque chose ? d'avance merci pour votre aide...

  5. #5
    Membre éclairé
    Homme Profil pro
    Ingénieur d'études en développements techniques
    Inscrit en
    Novembre 2005
    Messages
    244
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur d'études en développements techniques
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Novembre 2005
    Messages : 244
    Par défaut Ca avance mais ...
    ça avance bien, j'arrive à obtenir ce résultat :

    object(SimpleXMLElement)#199 (1) {
    ["COMMENT"] => array(3601) {
    [0] => object(SimpleXMLElement)#202 (1) {
    ["@attributes"] => array(6) {
    ["date"] => string(10) "2009-08-31"
    ["hotel"] => string(16) "TOTO2"
    ["note"] => string(41) "1:5-2:5-3:5-4:1-5:2-7:3-8:3-9:3-10:1-16:0"
    ["origin"] => string(5) "email"
    ["language"] => string(2) "fr"
    ["confirmationCode"] => string(16) "Ole750563cHQPHCM"
    }
    }
    [1] => object(SimpleXMLElement)#203 (1) {
    ["@attributes"] => array(6) {
    ["date"] => string(10) "2009-08-31"
    ["hotel"] => string(16) "TOTO1"
    ["note"] => string(41) "1:4-2:4-3:4-4:4-5:3-7:3-8:4-9:4-10:1-16:0"
    ["origin"] => string(5) "email"
    ["language"] => string(2) "en"
    ["confirmationCode"] => string(16) "Sch817884vQQPHCM"
    }
    }
    [2] => object(SimpleXMLElement)#204 (1) {
    ["@attributes"] => array(6) {
    ["date"] => string(10) "2009-09-01"
    ["hotel"] => string(16) "TOTO1"
    ["note"] => string(45) "1:5-2:5-3:5-4:2-5:5-6:2-7:5-8:4-9:4-10:1-16:0"
    ["origin"] => string(5) "email"
    ["language"] => string(2) "fr"
    ["confirmationCode"] => string(16) "Max931981dVQPHCM"
    }
    }
    [3] => object(SimpleXMLElement)#205 (1) {
    ["@attributes"] => array(9) {
    ["date"] => string(10) "2009-09-06"
    ["hotel"] => string(16) "TOTO2"
    ["note"] => string(66) "1:4-2:4-3:3-4:2-5:1-7:1-8:2-9:2-10:0-11:4-12:3-13:4-14:4-15:1-16:1"
    ["firstName"] => string(7) "Bernard"
    ["lastName"] => string(8) "salignon"
    ["country"] => string(2) "fr"
    ["origin"] => string(12) "distribution"
    ["language"] => string(2) "fr"
    ["confirmationCode"] => string(16) "sal610064dVQPHCM"
    }
    }
    obtenu grâce au code suivant :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
     
    /**
    	 * 
    	 *
    	 * @return arrReturn
    	 */
    	public function buildArray()
    	{
    		$arrReturn = simplexml_load_string(self::getComments());	
    		$count = count($arrReturn);	
    		$_tmpArray = array();
     
    		for($i=0;$i<$count;$i++){
    			$_tmpArray = $arrReturn[$i];
    			Zend_Debug::dump($_tmpArray);exit;			
    		}
    		return (object) $arrReturn;
    	}
    Je n'arrive pas à obtenir ce que je veux
    ["TOTO1"]
    ["fr"]
    ["date"] => string(10) "2009-09-06"
    ["note"] => string(66) "1:4-2:4-3:3-4:2-5:1-7:1-8:2-9:2-10:0-11:4-12:3-13:4-14:4-15:1-16:1"
    ["firstName"] => string(7) "Bernard"
    ["lastName"] => string(8) "salignon"
    ["origin"] => string(12) "distribution"
    ["confirmationCode"] => string(16) "sal610064dVQPHCM"

    ["en"]
    ["date"] => string(10) "2009-09-06"
    ["note"] => string(66) "1:4-2:4-3:3-4:2-5:1-7:1-8:2-9:2-10:0-11:4-12:3-13:4-14:4-15:1-16:1"
    ["firstName"] => string(7) "xxx"
    ["lastName"] => string(8) "yyyy"
    ["origin"] => string(12) "email"
    ["confirmationCode"] => string(16) "sal610064dVQPHCM"

    ["TOTO2"]
    ["fr"]
    ["date"] => string(10) "2009-09-06"
    ["note"] => string(66) "1:4-2:4-3:3-4:2-5:1-7:1-8:2-9:2-10:0-11:4-12:3-13:4-14:4-15:1-16:1"
    ["firstName"] => string(7) "Bernard"
    ["lastName"] => string(8) "salignon"
    ["origin"] => string(12) "distribution"
    ["confirmationCode"] => string(16) "sal610064dVQPHCM"

    ["en"]
    ["date"] => string(10) "2009-09-06"
    ["note"] => string(66) "1:4-2:4-3:3-4:2-5:1-7:1-8:2-9:2-10:0-11:4-12:3-13:4-14:4-15:1-16:1"
    ["firstName"] => string(7) "xxx"
    ["lastName"] => string(8) "yyyy"
    ["origin"] => string(12) "email"
    ["confirmationCode"] => string(16) "sal610064dVQPHCM"
    Après plusieurs essais infructueux, je n'arrive pas à obtenir ce que je veux, d'avance merci pour votre aide.

  6. #6
    Membre éclairé
    Homme Profil pro
    Ingénieur d'études en développements techniques
    Inscrit en
    Novembre 2005
    Messages
    244
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur d'études en développements techniques
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Novembre 2005
    Messages : 244
    Par défaut Voici le script final pour ceux que ça intéresse
    Finalement un très bon copain m'a donné la solution, voici le code pour ceux que ça intéresse :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
     
    /**
    	 * 
    	 *
    	 * @return arrReturn
    	 */
    	public function buildArray()
    	{	
    		$arrReturn = simplexml_load_string(self::getComments());
     
    		$arrComments = array();
    				foreach($arrReturn as $node) {
    					$strHotelId = null;
    					$strLocale = null;
    					$arrHotel = array();
    					foreach ($node->attributes() as $strName => $mxdValue) {
    						$strValue = (string) $mxdValue;
    						switch ($strName) {
    							case 'hotel':
    								$strHotelId = $strValue;
    								break;
     
    							case 'language':
    								$strLocale = $strValue;
    								break;
     
    							default:
    								$arrHotel[$strName] = $strValue;
    						}
     
    						if (null != $strHotelId && null != $strLocale) {
    							if (!isset($arrComments[$strHotelId])) {
    								$arrComments[$strHotelId] = array();
    							}
    							$arrComments[$strHotelId][$strLocale] = $arrHotel;
    						}
    					}
    				}
     
    		Zend_Debug::dump($arrComments);exit;
    		return $arrComments;
    	}

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Parser flux XML avec balise contenant ":" génère une erreur
    Par Pierrick81 dans le forum XML/XSL et SOAP
    Réponses: 3
    Dernier message: 22/08/2011, 19h19
  2. flux xml avec é : Invalid UTF8 encoding
    Par Sun03 dans le forum Format d'échange (XML, JSON...)
    Réponses: 10
    Dernier message: 07/02/2010, 19h12
  3. [XML] Comment envoyer un flux XML avec SOAP et PHP5 ?
    Par lehic dans le forum Bibliothèques et frameworks
    Réponses: 2
    Dernier message: 15/06/2007, 16h58
  4. [SimpleXML] Parser un flux xml avec SimpleXMLElement
    Par DeezerD dans le forum Bibliothèques et frameworks
    Réponses: 2
    Dernier message: 22/11/2006, 18h07

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo