dla ulatwienia podam ze zwracam request'a z naglowkiem Content-Type: text/plain (bo o to mi wlasciwie chodzi)
prototype to fajne narzedzie, musze jeszcze znalezc metode jak dostac sie do odebranego xml'a (kiedy tak bede zwracal). w ramach wymiany ponizej klaska w php jak znalazl do uzytku z Zend Framework i prototype (iterowalna, sluzy raczej jako kontener danych) do przygotowania Response. z zalozenia miala rowniez obslugiwac requesty ale to dopisze jak wreszcie rozgryze cala komunikacje jaka stosuje prototype.
prosze bardzo:
<?php
class wrx_Ajax_Prototype implements Countable, Iterator {
/**
* Iterator Patern
*
* @var int
*/
private $_index=0;
/**
* przechowuje obiekt simpleXML
*
* @var SimpleXMLElement
*/
private $_xml;
/**
* dane w formacie JSON
*
* @var string
*/
private $_json;
/**
* dane naturalne
*
* @var array
*/
private $_data;
/**
* naglowki dla konkretnych typow odpowiedzi
*
* @var array
*/
'xml'=>wrx_Mime::TYPE_XML , // application/xml
'json'=>wrx_Mime::TYPE_JSON , //application/x-json
'plain'=>wrx_Mime::TYPE_TEXT // text/plain
);
/**
* ustawiony typ
*
* @var string
*/
private $_type;
/**
* sprawdza czy żądanie jest wywolane przez ajax
*
* @return bool
*/
public static function isXMLHTTPRequest
(){ return ($_SERVER['HTTP_X_REQUESTED_WITH']=="XMLHttpRequest") ? true : false;
}
/**
* konstruktor i typ zwracanych wartosci
*
* @param string $type xml|json|plain
*/
public function __construct($type='xml'){
$this->_type=$type;
} else {
$this->_type='xml';
}
}
/**
* magiczna metoda wywolywana przy echo
*
*/
public function __toString(){
header('Content-Type: '.$this->_headers
[$this->_type
]); echo $this->returnContent(); }
/**
* zwraca gotowa tresc dokumentu w zaleznosci od typu
*
* @return string
*/
public function returnContent(){
$met='get'.$type;
return $this->$met();
}
/**
* zwraca dane w formacie JSON
*
* @return string
*/
public function getJSON(){
return Zend_Json::encode($this->_data);
}
/**
* zwraca dane w postaci XML
*
* @return string
*/
public function getXML(){
$xml=simplexml_load_file
(dirname(__FILE__).DIRECTORY_SEPARATOR
.'Prototype.xml'); $this->flatten2XML($this->_data,$xml);
//var_dump($xml);
return $xml->asXML();
}
/**
* zwraca dane jako text/plain
*
* @return string
*/
public function getPLAIN(){
$plain="";
$this->flatten2PLAIN($this->_data,$plain);
return $plain;
}
/**
* metoda zapewniajaca rekurencyjne zalatwienie zaleznosci
*
* @param array $node
* @param SimpleXMLElement $xml
*/
private function flatten2XML($node, SimpleXMLElement $xml){
foreach ($node as $k => $v) {
$this->flatten2XML($v,$xml->addChild($k));
} else {
$xml->addChild($k,(string) $v);
}
}
}
/**
* metoda zapewniajaca rekurencyjne zalatwienie zaleznosci
*
* @param array $node
* @param string $string
*/
private function flatten2PLAIN($node,&$string){
foreach ($node as $v) {
$this->flatten2PLAIN($v,$string);
} else {
$string.= (string) $v . "\n";
}
}
}
/**
* magiczna metoda do pobierania wartosci
*
* @param string $name
* @return mixed
*/
public function __get($name){
if (isset($this->_data
[$name])) { return $this->_data[$name];
}
}
/**
* magiczna metoda do zapisywania wartosci
*
* @param string $name
* @param mixed $value
*/
public function __set($name, $value){
$this->_data[$name]=$value;
}
/**
* Support isset() overloading on PHP 5.1
*
* @param string $name
* @return boolean
*/
protected function __isset($name)
{
return isset($this->_data
[$name]); }
/**
* Defined by Countable interface
*
* @return int
*/
{
return count($this->_data
); }
/**
* Defined by Iterator interface
*
* @return mixed
*/
{
}
/**
* Defined by Iterator interface
*
* @return mixed
*/
{
return key($this->_data
); }
/**
* Defined by Iterator interface
*
*/
{
$this->_index++;
}
/**
* Defined by Iterator interface
*
*/
{
$this->_index = 0;
}
/**
* Defined by Iterator interface
*
* @return boolean
*/
public function valid()
{
return $this->_index
< count($this->_data
); }
}
?>
Prototype.xml:
<?xml version='1.0' standalone='yes'?>
<response>
</response>