Catch json values in Curl return

Asked

Viewed 3,587 times

2

I have three different servers: server A, server B and server C. Where A registers users and stores personal information. I am trying to log in from B and C servers in A using Curl.

I need to return the A values to authenticate user and try to do this with an array and json_encode but I cannot rescue these values to store in php strings on B or C servers.

PHP Curl

<?php
header ('Content-type: text/html; charset=UTF-8');
if(!isset($_SESSION)) session_start(); 
if(isset($_POST['name'])){
   $name = $_POST['name'];
}
if($name == "Fulano"){
    $postData = array(
     'name' => $name
);
// Initialize cURL
$ch = curl_init();

// Setup options for a cURL transfer
curl_setopt_array(
    $ch, array(
    CURLOPT_URL => 'http://servidor requisitado/login_ext.php',
    CURLOPT_USERAGENT => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)',
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_COOKIEJAR => 'tmp/cookie.txt',
    CURLOPT_COOKIEFILE => 'tmp/cookie.txt',
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $postData,
    CURLOPT_RETURNTRANSFER, true
));

// Return web page
//return curl_exec($ch);

$server_output = curl_exec ($ch);

curl_close ($ch);

// further processing ....
if($server_output != ''){

   $string = json_decode($server_output, true);
    //???????????
   }

}else{
   echo "ERR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
}

PHP login_ext.php

<?php
header ('Content-type: text/html; charset=UTF-8');

if(isset($_POST['name'])){
   $name = $_POST['name'];
   //Verificação no Banco de dados!
   $nameRequest = $fetch['name'];// Ex: Fulano!
   $sobrenomeRequest = $fetch['sobrenome'];

   if($name == $nameRequest){

      $json = array("nome"=>$name, "sobrenome"=>$sobrenomeRequest);

      echo json_encode($json);
   }
}else{
   echo "Err";
}

I tried to make a foreach but without results!

What syntax to retrieve and use these values?

  • Where do you want to get the values? no "PHP Curl" where you have $string = json_decode($server_output, true); below put print_r($string);, see which way out.

  • The output is a json:{"name":"So-and-so","last name":"Ciclano"}1

  • These are the values I cannot assign to a php string...type ($returnName and $returnSobrenome) separately.

2 answers

2

One way to be redeeming these values is:

$string = json_decode($server_output);
$nome = $string->nome;
$sobrenome = $string->sobrenome;

echo $nome . " " . $sobrenome . "\n"; // Fulano Ciclano

See demonstração

  • This example serves on the same server but the array json comes from another server by Curl ... a var_dump() in the $name or $surname strings return "NULL"

  • @Lauromoraes can get something like this: echo $string->nome? or echo $string[0]['nome'].

  • echo $string->name returns error(Notice: Trying to get Property of non-object) and already in echo $string[0]['name'] does not return...doing a var_dump() it returns "NULL"

  • @Strange lauromoraes. What is the output of echo gettype($string);? Can you show a print of this command: print_r($string);?

  • gettype from "integer" and print_r from "11"

  • @Lauromoraes So that’s why it returns nothing. The variable $string that’s it $string = json_decode($server_output, true); correct? Try to access by object, I will update the reply.

  • For both strings the error is the same Notice: Trying to get Property of non-object

  • @Lauromoraes It worked?

Show 3 more comments

1

If I understand your question correctly this way will meet with what you need:

// Pega o json decodificado
$jsonDecodificado = json_decode($server_output, true);

foreach($jsonDecodificado as $key => $value){
         $nome = $value;
         echo $nome.'<br />';
}

Check if a certain key exists in the array:

if (array_key_exists("nome",$jsonDecodificado))
  {
  //faz uma validação;
  }

I am going through the resulting decoding array of json, so I can print it, compare it, make assignments.

  • Well I can not compare, I am trying to "rescue values" and assign to new strings in php separately and the example returned error (Parse error: syntax error, Unexpected 'as' (T_AS), expecting ';')

  • Sorry it was a foeach instead of for, test again.

  • Do you understand what I’ve done? I’m going through the resulting json decoding array, so I can print it, compare it, make assignments.

  • As I had said in the question I already tried a foreach and give this error (Warning: Invalid argument supplied for foreach() )

  • do so and see the summary: var_dump(json_decode($server_output, true)); see if an array comes out.

  • var_dump() in json_decode() returns {"name":"So-and-so","last name":"Ciclano"}int(1)

  • if it is really being returned {"nome":"Fulano","sobrenome":"Ciclano"}int(1) then the code is able to pick up the attributes.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.