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
Citation:
Message: Invalid id or tag 'xmlComment_cache_2011-35-30_fastbooking' : must use only [a-zA-Z0-9_]
Voici mon code :
Code:
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;
}
} |
Plus de message d'erreur mais le flux est vide
Je continue sur mon objet ZF,
voici le code obtenu :
Code:
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...
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:
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;
} |