par
ilanb » 01 août 2010, 02:05
Si j'ecris:
sortie:
Code : Tout sélectionner
{"posts":[{"post":{"post_title":null,"guid":"http:\/\/localhost\/wordpress\/?page_id=2"}},{"post":{"post_title":"Bonjour tout le monde!","guid":"http:\/\/localhost\/wordpress\/?p=1"}}]}
Si:
sortie:
Code : Tout sélectionner
{"posts":[{"post":{"post_title":"A propos \u00e0 jour","guid":"http:\/\/localhost\/wordpress\/?page_id=2"}},{"post":{"post_title":"Bonjour tout le monde!","guid":"http:\/\/localhost\/wordpress\/?p=1"}}]}
Si:
sortie:
Code : Tout sélectionner
{"posts":[{"post":{"post_title":"A propos ?our","guid":"http:\/\/localhost\/wordpress\/?page_id=2"}},{"post":{"post_title":"Bonjour tout le monde!","guid":"http:\/\/localhost\/wordpress\/?p=1"}}]}
et si je laisse:
sortie du xml:
Code : Tout sélectionner
Erreur d'analyse XML : mal formé
Emplacement : http://localhost/webservice/web-service.php?user=1&num=3
Numéro de ligne 1, Colonne 35 :<posts><post><post_title>A propos � jour</post_title><guid>http://localhost/wordpress/?page_id=2</guid></post><post><post_title>Bonjour tout le monde!</post_title><guid>http://localhost/wordpress/?p=1</guid></post></posts>
----------------------------------^
le script complet pour tester (avec wordpress):
<?php
header('Content-type: text/html; charset=UTF-8');
if(isset($_GET['user']) && intval($_GET['user'])) {
$number_of_posts = isset($_GET['num']) ? intval($_GET['num']) : 10;
$format = strtolower($_GET['format']) == 'json' ? 'json' : 'xml';
$user_id = intval($_GET['user']);
$link = mysql_connect('localhost','root','root') or die('erreur connex db');
mysql_select_db('wordpress',$link) or die('erreur select db);
$query = "SELECT post_title, guid FROM wp_posts WHERE post_author = $user_id AND post_status = 'publish' ORDER BY ID DESC LIMIT $number_of_posts";
$result = mysql_query($query,$link) or die('Errant query: '.$query);
$posts = array();
if(mysql_num_rows($result)) {
while($post = mysql_fetch_assoc($result)) {
//$posts[] = array('post'=>array_map('utf8_encode',$post));
$posts[] = array('post'=>$post);
}
}
if($format == 'json') {
header('Content-type: text/plain');
echo json_encode(array('posts'=>$posts));
}
else {
header('Content-type: text/xml');
echo '<posts>';
foreach($posts as $index => $post) {
if(is_array($post)) {
foreach($post as $key => $value) {
echo '<',$key,'>';
if(is_array($value)) {
foreach($value as $tag => $val) {
echo '<',$tag,'>',($val),'</',$tag,'>';
}
}
echo '</',$key,'>';
}
}
}
echo '</posts>';
}
@mysql_close($link);
}
?>
Si j'ecris:
[code]$posts[] = array('post'=>$post);[/code]
sortie:
[code]{"posts":[{"post":{"post_title":null,"guid":"http:\/\/localhost\/wordpress\/?page_id=2"}},{"post":{"post_title":"Bonjour tout le monde!","guid":"http:\/\/localhost\/wordpress\/?p=1"}}]}[/code]
Si:
[code]$posts[] = array('post'=>array_map('utf8_encode',$post));[/code]
sortie:
[code]{"posts":[{"post":{"post_title":"A propos \u00e0 jour","guid":"http:\/\/localhost\/wordpress\/?page_id=2"}},{"post":{"post_title":"Bonjour tout le monde!","guid":"http:\/\/localhost\/wordpress\/?p=1"}}]}[/code]
Si:
[code]$posts[] = array('post'=>array_map('utf8_decode',$post));[/code]
sortie:
[code]{"posts":[{"post":{"post_title":"A propos ?our","guid":"http:\/\/localhost\/wordpress\/?page_id=2"}},{"post":{"post_title":"Bonjour tout le monde!","guid":"http:\/\/localhost\/wordpress\/?p=1"}}]}[/code]
et si je laisse:
[code]$posts[] = array('post'=>$post);[/code]
sortie du xml:
[code]Erreur d'analyse XML : mal formé
Emplacement : http://localhost/webservice/web-service.php?user=1&num=3
Numéro de ligne 1, Colonne 35 :<posts><post><post_title>A propos � jour</post_title><guid>http://localhost/wordpress/?page_id=2</guid></post><post><post_title>Bonjour tout le monde!</post_title><guid>http://localhost/wordpress/?p=1</guid></post></posts>
----------------------------------^[/code]
le script complet pour tester (avec wordpress):
[php]<?php
header('Content-type: text/html; charset=UTF-8');
if(isset($_GET['user']) && intval($_GET['user'])) {
$number_of_posts = isset($_GET['num']) ? intval($_GET['num']) : 10;
$format = strtolower($_GET['format']) == 'json' ? 'json' : 'xml';
$user_id = intval($_GET['user']);
$link = mysql_connect('localhost','root','root') or die('erreur connex db');
mysql_select_db('wordpress',$link) or die('erreur select db);
$query = "SELECT post_title, guid FROM wp_posts WHERE post_author = $user_id AND post_status = 'publish' ORDER BY ID DESC LIMIT $number_of_posts";
$result = mysql_query($query,$link) or die('Errant query: '.$query);
$posts = array();
if(mysql_num_rows($result)) {
while($post = mysql_fetch_assoc($result)) {
//$posts[] = array('post'=>array_map('utf8_encode',$post));
$posts[] = array('post'=>$post);
}
}
if($format == 'json') {
header('Content-type: text/plain');
echo json_encode(array('posts'=>$posts));
}
else {
header('Content-type: text/xml');
echo '<posts>';
foreach($posts as $index => $post) {
if(is_array($post)) {
foreach($post as $key => $value) {
echo '<',$key,'>';
if(is_array($value)) {
foreach($value as $tag => $val) {
echo '<',$tag,'>',($val),'</',$tag,'>';
}
}
echo '</',$key,'>';
}
}
}
echo '</posts>';
}
@mysql_close($link);
}
?>[/php]