par
olistaps » 09 janv. 2021, 22:45
Bon et bien en faites le code....fonctionne (je suis vert d'avoir zappé cela, en tout cas merci beaucoup)
J'avais oublié plusieurs choses dans ma mise en forme
la réponse est :
{"code":1,"data":{"XXX":{"duration":"7","total":"10.752255","month":"1.168628","year":"1.168628","today":"0.180580","co2":"7.906070","tree":"0.395303","power":"13.000000","type":0,"capacity":"2.18"}},"message":"Réussir"}
et il me faudrait raccourcir ça c'est ok mais l'ajout des backlashing ne fonctionne pas pour au final donner cela :
{\"duration\":"10",\"total\":"12.197575",\"month\":"2.613948",\"year\":"2.613948",\"today\":"0.932200",\"co2\":"8.968805",\"tree\":"0.448440",\"power\":"10.000000",\"type\":0,\"capacity\":"2.18"}
J'ai fait cela ci dessous
Impossible d'ajouter un backlash en 2eme position et avant " autrement le reste est ok
<?php
$response = '{"code":1,"data":{"0b284800766b17bb0176a4ba048b64a2":{"duration":"10","total":"12.197575","month":"2.613948","year":"2.613948","today":"0.932200","co2":"8.968805","tree":"0.448440","power":"10.000000","type":0,"capacity":"2.18"}},"message":"Réussir"}';
$response = substr($response,53);
$response = substr($response,0,-23);
$response = substr_replace($response,'\\\\',1,0); => cela met deux \\ impossible 1 uniquement
$search = array("duration") ;
$replace = array("duration'\'");
$response1 = str_replace($search,$replace, $response);
$search = array("total") ;
$replace = array("total'\'");
$response2 = str_replace($search,$replace, $response1);
$search = array("month") ;
$replace = array("month'\'");
$response3 = str_replace($search,$replace, $response2);
$search = array("year") ;
$replace = array("year'\'");
$response4 = str_replace($search,$replace, $response3);
$search = array("today") ;
$replace = array("today'\'");
$response5 = str_replace($search,$replace, $response4);
$search = array("co2") ;
$replace = array("co2'\'");
$response6 = str_replace($search,$replace, $response5);
$search = array("tree") ;
$replace = array("tree'\'");
$response7 = str_replace($search,$replace, $response6);
$search = array("power") ;
$replace = array("power'\'");
$response8 = str_replace($search,$replace, $response7);
$search = array("type") ;
$replace = array("type'\'");
$response9 = str_replace($search,$replace, $response8);
$search = array("capacity") ;
$replace = array("capacity'\'");
$response10 = str_replace($search,$replace, $response9);
$response2 = str_replace("'","", $response10);
echo $response2;
?>
enfin puis directement utiliser cette commande dans une page php ou dois je passer autrement ?
mosquitto_pub -h localhost -t aps -m $response2
merci merci
Bon et bien en faites le code....fonctionne (je suis vert d'avoir zappé cela, en tout cas merci beaucoup)
J'avais oublié plusieurs choses dans ma mise en forme
la réponse est :
{"code":1,"data":{"XXX":{"duration":"7","total":"10.752255","month":"1.168628","year":"1.168628","today":"0.180580","co2":"7.906070","tree":"0.395303","power":"13.000000","type":0,"capacity":"2.18"}},"message":"Réussir"}
et il me faudrait raccourcir ça c'est ok mais l'ajout des backlashing ne fonctionne pas pour au final donner cela :
{\"duration\":"10",\"total\":"12.197575",\"month\":"2.613948",\"year\":"2.613948",\"today\":"0.932200",\"co2\":"8.968805",\"tree\":"0.448440",\"power\":"10.000000",\"type\":0,\"capacity\":"2.18"}
J'ai fait cela ci dessous
Impossible d'ajouter un backlash en 2eme position et avant " autrement le reste est ok
<?php
$response = '{"code":1,"data":{"0b284800766b17bb0176a4ba048b64a2":{"duration":"10","total":"12.197575","month":"2.613948","year":"2.613948","today":"0.932200","co2":"8.968805","tree":"0.448440","power":"10.000000","type":0,"capacity":"2.18"}},"message":"Réussir"}';
$response = substr($response,53);
$response = substr($response,0,-23);
$response = substr_replace($response,'\\\\',1,0); => cela met deux \\ impossible 1 uniquement
$search = array("duration") ;
$replace = array("duration'\'");
$response1 = str_replace($search,$replace, $response);
$search = array("total") ;
$replace = array("total'\'");
$response2 = str_replace($search,$replace, $response1);
$search = array("month") ;
$replace = array("month'\'");
$response3 = str_replace($search,$replace, $response2);
$search = array("year") ;
$replace = array("year'\'");
$response4 = str_replace($search,$replace, $response3);
$search = array("today") ;
$replace = array("today'\'");
$response5 = str_replace($search,$replace, $response4);
$search = array("co2") ;
$replace = array("co2'\'");
$response6 = str_replace($search,$replace, $response5);
$search = array("tree") ;
$replace = array("tree'\'");
$response7 = str_replace($search,$replace, $response6);
$search = array("power") ;
$replace = array("power'\'");
$response8 = str_replace($search,$replace, $response7);
$search = array("type") ;
$replace = array("type'\'");
$response9 = str_replace($search,$replace, $response8);
$search = array("capacity") ;
$replace = array("capacity'\'");
$response10 = str_replace($search,$replace, $response9);
$response2 = str_replace("'","", $response10);
echo $response2;
?>
enfin puis directement utiliser cette commande dans une page php ou dois je passer autrement ?
mosquitto_pub -h localhost -t aps -m $response2
merci merci