You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PHP CURL调用Polygon.io API无结果,但Postman请求正常

问题:PHP CURL调用Polygon.io API无返回结果,Postman可正常请求

此前用PHP CURL能正常调用Polygon.io各类API,今日调用接口时无返回结果。测试无参数的简单端点https://api.polygon.io/v1/marketstatus/now,执行后返回“No Result”,仅建立连接但无数据返回,Postman请求该端点则正常。

运行环境:WAMPserver + PHP 8.0.13,通过命令提示符执行。

测试代码

function callAPI($url) {
    $curl = curl_init();
    // Options
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        'Content-Type:application/json', 
        'Accept:application/json', 
        'Authorization: Bearer APIKEYasdf1234'
    ));
    // Execute
    $result = curl_exec($curl);
    if(!$result){
        if($_GET) {
            die('Connection Failure');
        }
        else {
            die('No result');
        }
    }
    curl_close($curl);
    return $result;
}
$url = 'https://api.polygon.io/v1/marketstatus/now';

// Execute Query
$get_data = callAPI($url);
$response = json_decode($get_data);

var_dump($response);

curl_getinfo输出结果

array(37) {
  ["url"]=>
  string(42) "https://api.polygon.io/v1/marketstatus/now"
  ["content_type"]=>
  NULL
  ["http_code"]=>
  int(0)
  ["header_size"]=>
  int(0)
  ["request_size"]=>
  int(0)
  ["filetime"]=>
  int(-1)
  ["ssl_verify_result"]=>
  int(20)
  ["redirect_count"]=>
  int(0)
  ["total_time"]=>
  float(0.069412)
  ["namelookup_time"]=>
  float(0.003393)
  ["connect_time"]=>
  float(0.033544)
  ["pretransfer_time"]=>
  float(0)
  ["size_upload"]=>
  float(0)
  ["size_download"]=>
  float(0)
  ["speed_download"]=>
  float(0)
  ["speed_upload"]=>
  float(0)
  ["download_content_length"]=>
  float(-1)
  ["upload_content_length"]=>
  float(-1)
  ["starttransfer_time"]=>
  float(0)
  ["redirect_time"]=>
  float(0)
  ["redirect_url"]=>
  string(0) ""
  ["primary_ip"]=>
  string(13) "[ip_address]"
  ["certinfo"]=>
  array(0) {
  }
  ["primary_port"]=>
  int(443)
  ["local_ip"]=>
  string(14) "[ip_address]"
  ["local_port"]=>
  int(60549)
  ["http_version"]=>
  int(0)
  ["protocol"]=>
  int(2)
  ["ssl_verifyresult"]=>
  int(0)
  ["scheme"]=>
  string(5) "HTTPS"
  ["appconnect_time_us"]=>
  int(0)
  ["connect_time_us"]=>
  int(33544)
  ["namelookup_time_us"]=>
  int(3393)
  ["pretransfer_time_us"]=>
  int(0)
  ["redirect_time_us"]=>
  int(0)
  ["starttransfer_time_us"]=>
  int(0)
  ["total_time_us"]=>
  int(69412)
}

尝试过的Postman生成代码(问题依旧)

function callAPI($url) {
    $curl = curl_init();
    // Options
    curl_setopt_array($curl, array(
      CURLOPT_URL => $url,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'GET',
      CURLOPT_HTTPHEADER => array(
        'Content-Type:application/json', 
            'Accept:application/json',
        'Authorization: Bearer APIKEYasdf1234'
      ),
    ));
    // Execute
    $result = curl_exec($curl);
    if(!$result){
        if($_GET) {
            die('Connection Failure');
        }
        else {
            die('No result');
        }
    }
    curl_close($curl);
    return $result;
}

解决方案

从curl_getinfo输出的ssl_verify_result: 20可以定位核心问题:无法验证SSL证书链的颁发者,说明你的PHP环境缺少Polygon.io SSL证书的信任根,导致SSL握手失败,无法获取响应数据。

具体解决步骤:

  1. 临时调试方案(跳过SSL验证,不推荐生产环境)
    在CURL配置中添加以下两行,强制跳过证书验证:

    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    

    测试是否能获取响应,若成功则确认是SSL证书信任问题。

  2. 生产环境正确配置(更新CA证书)

    • 下载Mozilla官方维护的CA证书包,保存为cacert.pem
    • 打开php.ini,找到curl.cainfo和openssl.cafile配置项,设置为证书文件的绝对路径:
      curl.cainfo = "C:/wamp64/bin/php/php8.0.13/extras/ssl/cacert.pem"
      openssl.cafile = "C:/wamp64/bin/php/php8.0.13/extras/ssl/cacert.pem"
      
    • 重启WAMP服务后重新测试API调用。
  3. 额外优化建议

    • 添加超时设置,避免请求无响应挂起:
      curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置30秒超时
      
    • 在curl_exec失败后输出CURL错误信息,方便后续排查:
      if(!$result){
          echo 'CURL错误:' . curl_error($curl);
          // 原有die逻辑...
      }
      

内容的提问来源于stack exchange,提问作者Alexnl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 08:54:24