【问题标题】:Using PHP's CURL to store API's JSON to variable使用 PHP 的 CURL 将 API 的 JSON 存储到变量中
【发布时间】:2014-08-09 01:22:35
【问题描述】:

我正在尝试将使用以下 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);

?>

调试输出为:

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' => 数组 (), 'primary_ip' => '10.137.36.11', 'primary_port' => 80, 'local_ip' => '', 'local_port' => 0, 'redirect_url' => '', ) curl 执行期间发生错误。补充资料:

【问题讨论】:

  • 请不要只说not executing properly 并谈论“问题”。显示结果、期望的结果、为什么让你感到惊讶、你的调试等等。

标签: php rest curl


【解决方案1】:

问题中的 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 执行时变量的无响应错误和空值。谢谢大家--
【解决方案2】:

我被告知该站点的 IP 地址已被非网络内的服务器阻止。这解决了 CURL 执行时变量的无响应错误和 null 值。

谢谢大家

【讨论】:

    猜你喜欢
    • 2014-02-21
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-26
    • 1970-01-01
    • 2018-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多