Issue
Friends, I have the problem of returning the endpoint https://api.bcb.gov.br/dados/serie/bcdata.sgs.4390/dados for the return by PHP.
In POSTMAN it works normal and the browser also normal.
Where is the problem?
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.bcb.gov.br/dados/serie/bcdata.sgs.4390/dados",
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(
"Authorization: Basic YXRoZW5hc3g6QHRoZW5hc1g=",
"Cookie: cookie_p=!scLarubhqjnExZBkmDwhOLi4iPSDyREMccf7/KajFSUEMTzB5Ayusi5+tGpJHXS2/gAiOR1B3EXRVmA=; TS01799025=0198c2d644c9f142b44ab6191be6416ca09c966281b4eac2e338095e981a329beb85787a3328474ab13c2f09d86baa6627e734caa34ceb49d8e51f2f880c249de254c0daa1"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
Thanx
Solution
From postman and browser it is working because you are loading code and file over https secured connection. If you are doing this on http or localhost you must add CURLOPT_SSL_VERIFYPEER => false
and CURLOPT_USERAGENT => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)'
to emulate browser and get results back.
But using CURLOPT_SSL_VERIFYPEER => false
is not really smart and leave you insecure, more about that option and other curl options you can read here https://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTSSLVERIFYPEER
Here is code i tested on localhost that works
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.bcb.gov.br/dados/serie/bcdata.sgs.4390/dados",
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(
"Authorization: Basic YXRoZW5hc3g6QHRoZW5hc1g=",
"Cookie: cookie_p=!scLarubhqjnExZBkmDwhOLi4iPSDyREMccf7/KajFSUEMTzB5Ayusi5+tGpJHXS2/gAiOR1B3EXRVmA=; TS01799025=0198c2d644c9f142b44ab6191be6416ca09c966281b4eac2e338095e981a329beb85787a3328474ab13c2f09d86baa6627e734caa34ceb49d8e51f2f880c249de254c0daa1"
),
// those 2 lines added
CURLOPT_USERAGENT => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)',
CURLOPT_SSL_VERIFYPEER => false,
));
$response = curl_exec($curl);
curl_close($curl);
// decode response
$obj = json_decode($response);
// output result
var_dump($obj);
Answered By - Mario Answer Checked By - David Marino (WPSolving Volunteer)