Search code examples
phpapigetxmlhttprequestphp-curl

Try check the output of the array sent by GET request


private $api_url= "www.myurl.com";
  private $api_key = "111abjkbjkvsdf3879";

private function getpage($url, $redirect=FALSE) {
  $url = $this->purge_url($url);
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
**/*Output the value of the array NULL NULL*/**
  $p_result = curl_exec($ch);
  $array = json_decode(trim($p_result), TRUE);
  var_dump($array);

  if ($redirect)  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');

 if (!$page=curl_exec($ch)) {
      $this->add_error("Remote GET connection to $url failed: ".curl_error($ch)); 
      curl_close($ch);
            return false;

  }
  curl_close($ch);

  $this->xmlsource = $page;

  return $page;
    } 

Output the value of the array in var_dump($array) is: NULL NULL Is the NULL in my case is the value or I did any mistake in my way to retrieve the value? I try to send APIkey request.


Solution

  • If what you get from var_export is actually an array with two values null then this corresponds to a successfull decode of the JSON [null, null].

    See this example:

    <?php
    $encoded = "[null, null]";
    
    $decoded = json_decode( $encoded, true );
    
    var_export( $decoded );
    

    outputs:

    array (
      0 => NULL,
      1 => NULL,
    )
    

    If this is not what you expect then you need apply corrections to the GET request.

    You can check it by inspecting $url that is passed to curl

    If you want to inspect what the response actually is before decoding use var_export( $p_result );