简体   繁体   English

通过 PHP CURL 从 Polygon.io ZDB974238714CA8DE634A7CE1D08 获得成功(通过 PostmanA1434A7CE1D08 响应成功)

[英]Getting no result via PHP CURL from Polygon.io API (getting successful response via Postman)

I having been using various Polygon.io API endpoints successfully via a PHP CURL request.我一直在通过 PHP ZA8BC924D0898064AB78Z85C 请求成功使用各种 Polygon.io API 端点。 However, today when I tried to run an endpoint, I got no results.但是,今天当我尝试运行端点时,我没有得到任何结果。 I have not been able to debug why.我一直无法调试原因。

I tried a simple endpoint with no parameters to test:我尝试了一个没有参数的简单端点来测试:

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

This gives me a No Result .这给了我一个No Result It made a successful connection and got nothing back.它建立了成功的连接,但没有得到任何回报。

When I try with Postman, I get the expected result.当我尝试使用 Postman 时,我得到了预期的结果。

在此处输入图像描述

Here is the Polygon documentation for this endpoint: https://polygon.io/docs/stocks/get_v1_marketstatus_now这是此端点的多边形文档: https://polygon.io/docs/stocks/get_v1_marketstatus_now

Running PHP 8.0.13 on WAMPserver.在 WAMPserver 上运行 PHP 8.0.13。 Executed via command prompt通过命令提示符执行

I did a var_dump of curl_getinfo($curl)) and here is what I got:我做了一个curl_getinfo($curl))的 var_dump,这就是我得到的:

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

I tried using the PHP - cURL code that Postman generates (and is successful with).我尝试使用 Postman 生成的 PHP - cURL 代码(并且成功)。 Here is my updated function:这是我更新的 function:

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

Unfortunately no change.可惜没有变化。

I am not sure what caused the issue.我不确定是什么导致了这个问题。 But I did the following to solve it:但我做了以下事情来解决它:

  1. I installed composer and guzzle我安装了 composer 和 guzzle
  2. used guzzle to make the request使用 guzzle 提出请求
  3. added a SSL cert添加了 SSL 证书

Here is the updated code:这是更新的代码:

function getGuzzleClient(array $options = [])
{
    $options = array_merge_recursive([
         'allow_redirects' => [
             'max' => 10,
         ],
         'base_uri' => 'https://api.polygon.io/',
         'verify'       => __DIR__.'/cacert.pem',
         'headers' => [
             'Accept' => 'application/json',
             'Authorization' => 'Bearer APIKEYasdf1234',
             'Content-Type' => 'application/json',
         ],
     ], $options);

    return new GuzzleHttp\Client($options);
}
$client = getGuzzleClient();

$response = $client->get('v1/marketstatus/now');

var_dump(
    json_decode($response->getBody(), true)
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM