0

How can I read this result using PHP? This is print_r of a result from a request:

object(PhpSigep\Services\Result)[687]
  protected 'isSoapFault' => boolean false
  protected 'errorCode' => null
  protected 'errorMsg' => null
  protected 'result' => 
    array (size=38)
      0 => 
        object(PhpSigep\Model\CalcPrecoPrazoResposta)[730]
          protected 'servico' => 
            object(PhpSigep\Model\ServicoDePostagem)[731]
              ...
          protected 'valor' => float 12.28
          protected 'prazoEntrega' => int 5
          protected 'valorMaoPropria' => float 0
          protected 'valorAvisoRecebimento' => float 0
          protected 'valorValorDeclarado' => float 0
          protected 'entregaDomiciliar' => boolean true
          protected 'entregaSabado' => boolean false
          protected 'erroCodigo' => int 0
          protected 'erroMsg' => null
          protected '_failIfAtributeNotExiste' => boolean true
      1 => 
        object(PhpSigep\Model\CalcPrecoPrazoResposta)[732]
          protected 'servico' => 
            object(PhpSigep\Model\ServicoDePostagem)[733]
              ...
          protected 'valor' => float 22.9
          protected 'prazoEntrega' => int 5
          protected 'valorMaoPropria' => float 0
          protected 'valorAvisoRecebimento' => float 0
          protected 'valorValorDeclarado' => float 0
          protected 'entregaDomiciliar' => boolean true
          protected 'entregaSabado' => boolean false
          protected 'erroCodigo' => int 0
          protected 'erroMsg' => null
          protected '_failIfAtributeNotExiste' => boolean true

I would like to read the array, I am trying $result->result, $result['result'] and other combinations, but I really didn't manage to read it. Can someone help me, please?

Rodrigo
  • 11
  • 2

1 Answers1

0
  • First , verify if there is public method like getResult() on the class definition.

  • In case you cannot modify the class definition , use Reflection like this way :

Code example:

function accessProtected($obj, $prop) {
  $reflection = new ReflectionClass($obj);
  $property = $reflection->getProperty($prop);
  $property->setAccessible(true);
  return $property->getValue($obj);
}

Calling the method :

$data = accessProtected($result,"result");

To access the data indise , call the function recursively .

if you want to get valor and prazoEntrega

foreach($data as $CalcPrecoPrazoResposta){
    $valor = accessProtected($CalcPrecoPrazoResposta,"valor");
    $prazoEntrega = accessProtected($CalcPrecoPrazoResposta,"prazoEntrega");
  echo $valor;
  echo $prazoEntrega;
}
Yassine CHABLI
  • 3,459
  • 2
  • 23
  • 43