Welcome Guest, Not a member yet? Register   Sign In
curl->simple_get json
#1

I have proble using curl simple_get
when trying to get json, it always return bool(false)
when I tried the api address in browser, they works just fine


Code:
  public function test_curl()
  {

$response = (object)null;

$data = json_decode($this->curl->simple_get('any_json_api_address'));
   
    if (!empty($data))
    {
      $response->isSuccess = TRUE;
      $response->message = 'success.';
      $response->data = $data;
    }
    else
    {
      $response->isSuccess = FALSE;
      $response->message = 'failed.';
    }

    echo json_encode($response);
  }
Reply
#2

PHP Code:
json_decode(
    string $json,
    ?bool $associative null,
    int $depth 512,
    int $flags 0
): mixed 

associative

When true, JSON objects will be returned as associative arrays; when false, JSON objects will be returned as objects.
When null, JSON objects will be returned as associative arrays or objects depending on whether JSON_OBJECT_AS_ARRAY
is set in the flags.

PHP Code:
// your missing true for the associated array flag.
$data json_decode($this->curl->simple_get('any_json_api_address'));

// should be
$data json_decode($this->curl->simple_get('any_json_api_address'), true); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB