I having been using various Polygon.io API endpoints successfully via a PHP CURL request. 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
. It made a successful connection and got nothing back.
When I try with Postman, I get the expected result.
Here is the Polygon documentation for this endpoint: https://polygon.io/docs/stocks/get_v1_marketstatus_now
Running PHP 8.0.13 on WAMPserver. Executed via command prompt
I did a var_dump of curl_getinfo($curl))
and here is what I got:
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). Here is my updated 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.
CodePudding user response:
I am not sure what caused the issue. But I did the following to solve it:
- I installed composer and guzzle
- used guzzle to make the request
- added a SSL cert
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)
);