doushui20090526 2014-06-18 16:32
浏览 63

使用PHP的CURL将API的JSON存储到变量

I'm trying to store data exposed with the following URL in a PHP variable ($curl_response) for further manipulation, but my current code is not executing properly. I've copied/pasted the data exposed in the following URL into the body of an HTML file and have tried running the below script on that HTML file, and CURL works properly. I'm guessing the issue then has to do with getting a response correctly from this particular site. Perhaps there is a CURL option I'm overlooking.

http://tdm.prod.obanyc.com/api/pullouts/agency/MTABC/list

Thoughts?

<?php 

$ch = curl_init("http://tdm.prod.obanyc.com/api/pullouts/agency/MTABC/list");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($ch);

if ($curl_response === false) {
  $info = curl_getinfo($ch);
  curl_close($ch);
  die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
else echo $curl_response ;

curl_close($ch);

?>

The debugging output is:

array ( 'url' => '', 'content_type' => NULL, 'http_code' => 0, 'header_size' => 0, 'request_size' => 0, 'filetime' => -1, 'ssl_verify_result' => 0, 'redirect_count' => 0, 'total_time' => 63.145232, 'namelookup_time' => 0.006015, 'connect_time' => 0, 'pretransfer_time' => 0, 'size_upload' => 0, 'size_download' => 0, 'speed_download' => 0, 'speed_upload' => 0, 'download_content_length' => -1, 'upload_content_length' => -1, 'starttransfer_time' => 0, 'redirect_time' => 0, 'certinfo' => array ( ), 'primary_ip' => '10.137.36.11', 'primary_port' => 80, 'local_ip' => '', 'local_port' => 0, 'redirect_url' => '', )error occured during curl exec. Additioanl info:

  • 写回答

2条回答 默认 最新

  • donglu9445 2014-06-18 16:39
    关注

    Is your else statement in the question the same way it is in the code? If yes, then I would start by fixing the else statement

    Change

    else $curl_response ;
    

    to

    else
    {
       $curl_response; // You might want to echo  $curl_response or store it or do something with it. In it's current state it just does nothing.
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办