CKTIS / PHP
Entrada, salida y serialización
Un flujo tiene posición. Tras escribir JSON en un flujo reposicionable ejecute rewind antes de leer.
Versión: PHP 8.4; repository runtime, production runtime not attested
Sintaxis
fopen("php://memory", "w+"); rewind($stream);Un flujo tiene posición. Tras escribir JSON en un flujo reposicionable ejecute rewind antes de leer. JSON_THROW_ON_ERROR distingue errores de sintaxis o codificación de un null válido. associative=true decodifica objetos JSON como arrays PHP. Cierre el recurso al terminar.
$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"]
Ejemplos
<?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)]);}
Resultado
["","007"] [null,"JsonException"]