ghostrider
ghostrider

Reputation: 5259

getting JSON object from a GET cURL in php

Here is my code:

$ch = curl_init('');  

  $request = 'where={"place":"'.$place.'"}&count=1&limit=0';
  $url = "https://api.parse.com/1/classes/className" . "?" . $request;

  curl_setopt($ch, CURLOPT_URL,$url);



  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'X-Parse-Application-Id: ...',
    'X-Parse-REST-API-Key: ...
    ));

  //curl_setopt($ch, CURLOPT_POST, true);


  // Execute
  $result = curl_exec($ch);

  // Close connection
  curl_close($ch);

Here is the output:

{"results":[],"count":0}

I want to get only he number in count.

I tried this:

$json_res = json_decode($result, true);

  echo 'Number : '.$json_res['count'];

but the output becomes:

Number :

If i do json_encode the result is just a True value.

I tried added this:

  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

but didn't change anything.

What am I missing?

EDIT: doing a var_dump on $json_res gives me that: int(1). Why is that?

Upvotes: 1

Views: 1203

Answers (1)

Alex Barroso
Alex Barroso

Reputation: 859

Curl does not return the output if you don't set the CURLOPT_RETURNTRANSFER option

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

Upvotes: 1

Related Questions