Traiter les informations d'un fichier JSON dans PHP
Bonjour,
J'ai a disposition un fichier JSON contenant une liste de serveurs et leur descriptif dans lequel je souhaiterai extraire le nom afin de vérifier s'il est bien présent dans un autre fichier contenant une autre liste de serveur.
L'objectif étant d'établir une liste de serveurs présent dans le fichier JSON mais ne figurants pas dans le second fichier (pas le contraire).
J'ai pu établir la structure du fichier JSON via le script ci-dessous :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
<?php
function lecture($fic){
$json = file_get_contents("$fic");
//lire la structure du fichier
var_dump(json_decode($json), false);
}
$fic = '/home/titi/monfichier.json';
//$fi='http://monserveur/prod/object?etat=rapport_001array'
lecture($fic);
?> |
La structure affichée est la suivant :
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
|
<a href="http://monserveur/prod/object?etat=rapport_001array(6)" target="_blank">http://monserveur/prod/object?etat=rapport_001array(6)</a> {
[0]=>
object(stdClass)#1 (9) {
["libelle_long"]=>
string(0) ""
["statut"]=>
string(5) "Actif"
["os"]=>
string(20) "Appliance logicielle"
["classification"]=>
string(8) "SI_Infra"
["intitule"]=>
string(13) "Proxy Interne"
["fabricant"]=>
string(6) "Vmware"
["nom"]=>
string(9) "srvp001"
["environnement"]=>
string(12) "P Production"
["localisation"]=>
string(35) "Tours DataCenter\Virtualisation"
}
[1]=>
object(stdClass)#2 (9) {
["libelle_long"]=>
string(0) ""
["statut"]=>
string(5) "Actif"
["os"]=>
string(14) "Non Applicable"
["classification"]=>
string(16) "Non_Reautonomise"
["intitule"]=>
string(30) "Avocent PDU Power Device Unit"
["fabricant"]=>
string(7) "Avocent"
["nom"]=>
string(16) "pdu5-dcb-xxxxx1"
["environnement"]=>
string(12) "X Production"
["localisation"]=>
string(22) "Marseille DataCenterB"
}
[2]=>
object(stdClass)#3 (9) {
["libelle_long"]=>
string(0) ""
["statut"]=>
string(9) "Supprimé"
["os"]=>
string(0) ""
["classification"]=>
string(17) "ReLocalisation"
["intitule"]=>
string(39) "BDD SQL 2005"
["fabricant"]=>
string(0) ""
["nom"]=>
string(10) "srvw-185"
["environnement"]=>
string(12) "X Production"
["localisation"]=>
string(0) ""
}
[3]=>
object(stdClass)#4 (9) {
["libelle_long"]=>
string(0) ""
["statut"]=>
string(5) "Actif"
["os"]=>
string(5) "Linux"
["classification"]=>
string(5) "SI_PROD"
["intitule"]=>
string(40) "Zone echange fichier de production"
["fabricant"]=>
string(6) "Vmware"
["nom"]=>
string(11) "vxlnfs01"
["environnement"]=>
string(12) "X Production"
["localisation"]=>
string(35) "Marseille DataCenter\Virtualisation"
}
[4]=>
object(stdClass)#5 (9) {
["libelle_long"]=>
string(0) ""
["statut"]=>
string(5) "Actif"
["os"]=>
string(5) "Linux"
["classification"]=>
string(9) "SICentral"
["intitule"]=>
string(49) "Serveur Splunk."
["fabricant"]=>
string(6) "Vmware"
["nom"]=>
string(12) "srvx004"
["environnement"]=>
string(12) "X Production"
["localisation"]=>
string(35) "Tours DataCenter\Virtualisation"
}
[5]=>
object(stdClass)#6 (9) {
["libelle_long"]=>
string(0) ""
["statut"]=>
string(5) "Actif"
["os"]=>
string(5) "Linux"
["classification"]=>
string(5) "Métier"
["intitule"]=>
string(17) "Apache prod"
["fabricant"]=>
string(6) "Vmware"
["nom"]=>
string(23) "srvx1@domaine.com"
["environnement"]=>
string(12) "X Production"
["localisation"]=>
string(35) "Marseille DataCenter\Virtualisation"
}
} |
Mon second fichier contenant une liste de serveur est le suivant :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
cat /home/titi/maliste.txt
srvx1;1;005
srvx4@domaine.com;2;009
srvx5;3;007
srvx8@domaine.com;4;010
srvx9@domaine.com;5;088
srvx004@domaine.com;8;077
vxlnfs01;6;070
pdu5-dcb-xxxxx1;8;89
srvp001;10;98
pdu5-dcb-xxxxx2@domaine.fr;12;69 |
L'objectif serait d'extraire dans le fichier JSON le champs nom des serveurs actifs afin de vérifier s'ils sont bien présents dans le fichier maliste.txt.
A noter que le nom du serveur n'est pas forcément nommé de manière identique dans chaque fichier :
exemple :
Fichier JSON => srvx1@domaine.com
Fichier liste serveur => srvx1
Espérant avoir été assez précis dans ma demande et vous remerciant par avance pour votre aide.
cordialement,