CKTIS / PHP
Entrées, sorties et sérialisation
Un flux a une position. Après écrire du JSON dans un flux repositionnable, faites rewind avant lecture.
Version : PHP 8.4; repository runtime, production runtime not attested
Syntaxe
fopen("php://memory", "w+"); rewind($stream);Un flux a une position. Après écrire du JSON dans un flux repositionnable, faites rewind avant lecture. JSON_THROW_ON_ERROR distingue les erreurs de syntaxe ou d’encodage d’un null valide. associative=true décode les objets en tableaux PHP. Fermez la ressource après usage.
$fp=fopen("php://memory","w+");fwrite($fp,json_encode(["sku"=>"007"],JSON_THROW_ON_ERROR));$atEnd=stream_get_contents($fp);rewind($fp);$row=json_decode(stream_get_contents($fp),true,512,JSON_THROW_ON_ERROR);fclose($fp);echo json_encode([$atEnd,$row["sku"]]);
$null=json_decode("null",true,512,JSON_THROW_ON_ERROR);try{json_decode("{",true,512,JSON_THROW_ON_ERROR);}catch(JsonException $e){echo "\n",json_encode([$null,get_class($e)]);}
["","007"]
[null,"JsonException"]
Exemples
<?php
$fp=fopen("php://memory","w+");fwrite($fp,json_encode(["sku"=>"007"],JSON_THROW_ON_ERROR));$atEnd=stream_get_contents($fp);rewind($fp);$row=json_decode(stream_get_contents($fp),true,512,JSON_THROW_ON_ERROR);fclose($fp);echo json_encode([$atEnd,$row["sku"]]);
$null=json_decode("null",true,512,JSON_THROW_ON_ERROR);try{json_decode("{",true,512,JSON_THROW_ON_ERROR);}catch(JsonException $e){echo "\n",json_encode([$null,get_class($e)]);}
Résultat
["","007"] [null,"JsonException"]