Bonjour
une fonction qui me permet de récupérer des éléments d'un POST dans un JSON se déroule correctement sous PHP 5.5 /7 le serveur sur lequel je dois mettre le site est en PHP 5.3 et je ne récupère rien.
Le code d'origine qui fonctionne sous PHP >5.3
Je rajoute un test du JSON
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 <?php include 'function.php'; // include 'template/header.php'; print_r($_POST['media']); $diaporama = $_GET['diaporama']; echo $diaporama; deleteAllImg('photos', $diaporama); $array = json_decode($_POST['media'], true); foreach ($array as $key => $value) { $data = array( 'diaporama' => $value['diaporama'], 'titre' => $value['titre'], 'numero' => $value['numero'], 'chemin' => $value['chemin'] ); ajoutBase("photos", $data); } ?>et j'enlève des caractères invisible fonction trouvé sur stack overflow
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 switch (json_last_error()) { case JSON_ERROR_NONE: echo ' - Aucune erreur'; break; case JSON_ERROR_DEPTH: echo ' - Profondeur maximale atteinte'; break; case JSON_ERROR_STATE_MISMATCH: echo ' - Inadéquation des modes ou underflow'; break; case JSON_ERROR_CTRL_CHAR: echo ' - Erreur lors du contrôle des caractères'; break; case JSON_ERROR_SYNTAX: echo ' - Erreur de syntaxe ; JSON malformé'; break; case JSON_ERROR_UTF8: echo ' - Caractères UTF-8 malformés, probablement une erreur d\'encodage'; break; default: echo ' - Erreur inconnue'; break; }
Cette fonction me permet de ne plus avoir d'erreur de syntaxe dans le decode JSON
le code final est donc ça
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 $checkLogin = file_get_contents($_POST['media']); // This will remove unwanted characters. // Check http://www.php.net/chr for details for ($i = 0; $i <= 31; ++$i) { $checkLogin = str_replace(chr($i), "", $checkLogin); } $checkLogin = str_replace(chr(127), "", $checkLogin); // This is the most common part // Some file begins with 'efbbbf' to mark the beginning of the file. (binary level) // here we detect it and we remove it, basically it's the first 3 characters if (0 === strpos(bin2hex($checkLogin), 'efbbbf')) { $checkLogin = substr($checkLogin, 3); }
Le test du Decode Json renvoie aucune erreur
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 <?php include 'function.php'; // include 'template/header.php'; print_r($_POST['media']); $diaporama = $_GET['diaporama']; echo $diaporama; deleteAllImg('photos', $diaporama); $checkLogin = file_get_contents($_POST['media']); // This will remove unwanted characters. // Check http://www.php.net/chr for details for ($i = 0; $i <= 31; ++$i) { $checkLogin = str_replace(chr($i), "", $checkLogin); } $checkLogin = str_replace(chr(127), "", $checkLogin); // This is the most common part // Some file begins with 'efbbbf' to mark the beginning of the file. (binary level) // here we detect it and we remove it, basically it's the first 3 characters if (0 === strpos(bin2hex($checkLogin), 'efbbbf')) { $checkLogin = substr($checkLogin, 3); } $array= json_decode($_POST['media'], true); switch (json_last_error()) { case JSON_ERROR_NONE: echo ' - Aucune erreur'; break; case JSON_ERROR_DEPTH: echo ' - Profondeur maximale atteinte'; break; case JSON_ERROR_STATE_MISMATCH: echo ' - Inadéquation des modes ou underflow'; break; case JSON_ERROR_CTRL_CHAR: echo ' - Erreur lors du contrôle des caractères'; break; case JSON_ERROR_SYNTAX: echo ' - Erreur de syntaxe ; JSON malformé'; break; case JSON_ERROR_UTF8: echo ' - Caractères UTF-8 malformés, probablement une erreur d\'encodage'; break; default: echo ' - Erreur inconnue'; break; } echo "afficher le ".$array; print_r($array); foreach ($array as $key => $value) { $data = array( 'diaporama' => $value['diaporama'], 'titre' => $value['titre'], 'numero' => $value['numero'], 'chemin' => $value['chemin'] ); ajoutBase("photos", $data); } ?>
mais les test de la variable $Array ligne 58 et 59 ne renvoie rien sous PHP 5.3 et sont correctes à partir d'une version 5.5
Partager