drmcm84800 2014-01-11 22:50
浏览 90
已采纳

从PHP中的GET cURL获取JSON对象

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?

  • 写回答

1条回答 默认 最新

  • dongmang3961 2014-01-11 22:58
    关注

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

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?