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


Using PHP's CURL to store API's JSON to variable

我试图将以下URL暴露的数据存储在PHP变量($curl_response)中以供进一步操作,但我当前的代码没有正确执行。我将以下URL中暴露的数据复制/粘贴到HTML文件的主体中,并尝试在该HTML文件上运行以下脚本,CURL工作正常。我猜这个问题必须从这个特定的网站得到正确的回应。也许我忽略了一个CURL选项。

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

想法吗?

<?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);
?>

调试输出为:

阵列(url ' => '', ' content_type ' =>空,"http_code"=> 0,' header_size"=> 0,' requestrongize"=> 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' => ",) curl执行时发生错误。Additioanl信息:

问题中的else语句是否与代码中的相同?如果是,那么我将从修复else语句

开始

改变
else $curl_response ;

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.
}

我被告知,该网站的IP地址被阻止服务器不在网络中。这解决了CURL执行时的无响应错误和变量的空值。

谢谢所有的