I am trying to print data from an api, and I am not able to. It shows me several fields and I only want to be able to print the name. I attach code
THANKS IN ADVANCE
header('Content-Type: application/json');
function actionIndex(){
$error = '';
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERPWD, 'user:926724');
curl_setopt($ch, CURLOPT_URL, 'https://pruebas.donquijote.com/api/clientes/1');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_exec($ch);
$error = curl_errno($ch);
if($error){
echo ("Error: ".$error);
}
}
$var = actionIndex();
echo $var['nombre'];
//Esto me imprime
//{"id": 2,"clienteid": "00001","nombre": "Pepe"}
//Pero lo que quiero es imprimir solo el nombre, algo así como echo $var["nombre"]
?>
What you are getting is a
string
in formatjson
which you have todecodificar
(converting it to aobjeto
) and then converting theobjeto
toarray
(I would use acasteo
):With this I would solve your question but I see that you have other possible
errores
syntaxes,php
whenever you call a method and want to obtain data it must have areturn
, youcódigo
should look like this: