JSON est assez "standard", utilisable avec php mais aussi avec JS ou java (et surement d'autre langage).
ça ressemble a ça :
$a = json_encode(array('<foo>',"'bar'",'"baz"','&blong&', "\xc3\xa9"));
donne ["<foo>","'bar'","\"baz\"","&blong&","\u00e9"]
le format est simple, en php si tu récupère ça, un json_decode et tu obtient un tableau.
Si tu avais un objet tu peux le récupérer ainsi.
Pas de prise de tête. Imagine que tu modélise tes fiches via un objet.
par exemple
<?php
class fiche implements JsonSerializable{
private $numero;
private $titre;
private $text;
// d'autre trucs si tu veux
public function __construct(){
//
}
public function getNumero(){
return $this->numero;
}
public function getTitre(){
return $this->titre;
}
public function getText(){
return $this->text;
}
public function setNumero($num){
$this->numero = $num;
}
public function setTitre($titre){
$this->titre=$titre;
}
public function setText($txt){
$this->text = $txt;
}
public function jsonSerialize(){
return ['numero'=>$this->getNumero(),'texte'=>$this->getText(),'titre'=>$this->getTitre()];
}
}
$f1 = new fiche();
$f1->setNumero(12);
$f1->setText('du blabla
sur plusieurs
meme si
tu veux');
$f1->setTitre('le titre');
$encode = json_encode($f1);
var_dump($encode);
$decode = json_decode($encode);
var_dump($decode);
exemple basique sans contrôle de champs et surement pas adapter
donne
Code : Tout sélectionner
string '{"numero":12,"texte":"du blabla \r\n\t\tsur plusieurs\r\nmeme si \r\n\t\ttu veux","titre":"le titre"}' (length=100)
object(stdClass)[2]
public 'numero' => int 12
public 'texte' => string 'du blabla
sur plusieurs
meme si
tu veux' (length=47)
public 'titre' => string 'le titre' (length=8)
Que ce soit avec du xml que tu formate ou un tableau / objet (je verrais un tableau d'objet fiche

) que tu encode en json au final tu final tu fait un "simple" echo.
pour un exemple précis a partir de mon 1er code
le fichier de ton serveur
<?php
class fiche implements JsonSerializable{
private $numero;
private $titre;
private $text;
// d'autre trucs si tu veux
public function __construct(){
//
}
public function getNumero(){
return $this->numero;
}
public function getTitre(){
return $this->titre;
}
public function getText(){
return $this->text;
}
public function setNumero($num){
$this->numero = $num;
}
public function setTitre($titre){
$this->titre=$titre;
}
public function setText($txt){
$this->text = $txt;
}
public function jsonSerialize(){
return ['numero'=>$this->getNumero(),'texte'=>$this->getText(),'titre'=>$this->getTitre()];
}
}
$f1 = new fiche();
$f1->setNumero(12);
$f1->setText('du blabla
sur plusieurs
meme si
tu veux');
$f1->setTitre('le titre');
$encode = json_encode($f1);
echo $encode;
le fichier "distant"
<?php
$encode = file_get_contents('http://localhost/test/juliette2.php');
$decode = json_decode($encode);
var_dump($decode);
affiche
Code : Tout sélectionner
object(stdClass)[1]
public 'numero' => int 12
public 'texte' => string 'du blabla
sur plusieurs
meme si
tu veux' (length=47)
public 'titre' => string 'le titre' (length=8)
ce que l'on avait avant
c'est pas plus complexe que cela.
tu peux regarder un exemple
A cette adresse (le code source est en bas de page)
Cela reste rudimentaire mais devrais t'aider un peu
@+
[url=http://wwwhp.net/json/]JSON[/url] est assez "standard", utilisable avec php mais aussi avec JS ou java (et surement d'autre langage).
ça ressemble a ça :
$a = json_encode(array('<foo>',"'bar'",'"baz"','&blong&', "\xc3\xa9"));
donne ["<foo>","'bar'","\"baz\"","&blong&","\u00e9"]
le format est simple, en php si tu récupère ça, un json_decode et tu obtient un tableau.
Si tu avais un objet tu peux le récupérer ainsi.
Pas de prise de tête. Imagine que tu modélise tes fiches via un objet.
par exemple
[php]
<?php
class fiche implements JsonSerializable{
private $numero;
private $titre;
private $text;
// d'autre trucs si tu veux
public function __construct(){
//
}
public function getNumero(){
return $this->numero;
}
public function getTitre(){
return $this->titre;
}
public function getText(){
return $this->text;
}
public function setNumero($num){
$this->numero = $num;
}
public function setTitre($titre){
$this->titre=$titre;
}
public function setText($txt){
$this->text = $txt;
}
public function jsonSerialize(){
return ['numero'=>$this->getNumero(),'texte'=>$this->getText(),'titre'=>$this->getTitre()];
}
}
$f1 = new fiche();
$f1->setNumero(12);
$f1->setText('du blabla
sur plusieurs
meme si
tu veux');
$f1->setTitre('le titre');
$encode = json_encode($f1);
var_dump($encode);
$decode = json_decode($encode);
var_dump($decode);
[/php]
exemple basique sans contrôle de champs et surement pas adapter
donne [code]string '{"numero":12,"texte":"du blabla \r\n\t\tsur plusieurs\r\nmeme si \r\n\t\ttu veux","titre":"le titre"}' (length=100)
object(stdClass)[2]
public 'numero' => int 12
public 'texte' => string 'du blabla
sur plusieurs
meme si
tu veux' (length=47)
public 'titre' => string 'le titre' (length=8)[/code]
Que ce soit avec du xml que tu formate ou un tableau / objet (je verrais un tableau d'objet fiche :) ) que tu encode en json au final tu final tu fait un "simple" echo.
pour un exemple précis a partir de mon 1er code
le fichier de ton serveur
[php]<?php
class fiche implements JsonSerializable{
private $numero;
private $titre;
private $text;
// d'autre trucs si tu veux
public function __construct(){
//
}
public function getNumero(){
return $this->numero;
}
public function getTitre(){
return $this->titre;
}
public function getText(){
return $this->text;
}
public function setNumero($num){
$this->numero = $num;
}
public function setTitre($titre){
$this->titre=$titre;
}
public function setText($txt){
$this->text = $txt;
}
public function jsonSerialize(){
return ['numero'=>$this->getNumero(),'texte'=>$this->getText(),'titre'=>$this->getTitre()];
}
}
$f1 = new fiche();
$f1->setNumero(12);
$f1->setText('du blabla
sur plusieurs
meme si
tu veux');
$f1->setTitre('le titre');
$encode = json_encode($f1);
echo $encode;[/php]
le fichier "distant"
[php]<?php
$encode = file_get_contents('http://localhost/test/juliette2.php');
$decode = json_decode($encode);
var_dump($decode);
[/php]
affiche
[code]object(stdClass)[1]
public 'numero' => int 12
public 'texte' => string 'du blabla
sur plusieurs
meme si
tu veux' (length=47)
public 'titre' => string 'le titre' (length=8)[/code]
ce que l'on avait avant :)
c'est pas plus complexe que cela.
tu peux regarder un exemple [url=http://phpjungle.info/phpfrance/juliette/]A cette adresse[/url] (le code source est en bas de page)
Cela reste rudimentaire mais devrais t'aider un peu :)
@+