Reputation: 11
I have to access an API using a GET request but the result is:
301 Moved Permanently.
Here is my code:
$curl = curl_init();
$url="https://xxx.yyy.com/zzz/v1.0";
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: Bearer sadhuUasjhda"
),
));
$result = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
print_r($result);exit;
I've read that this problem can be solve by adding CURLOPT_POSTREDIR
, but that's only worked when I used POST.
I've tried to use CURLOPT_FOLLOWLOCATION
but I get a different problem, the result is like
CURL Error #:SSL certificate problem: unable to get local issuer certificate.
FYI, I'm using Windows and XAMPP. And then I tried following instructions on this post but the result is still the same.
Upvotes: 0
Views: 3941
Reputation: 11
thanks to Rendy Eko Prastiyo, the solution is add CURLOPT_SSL_VERIFYPEER set to false and CURLOPT_FOLLOWLOCATION set to true, so the code is
$curl = curl_init();
$url="https://xxx.yyy.com/zzz/v1.0";
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTPHEADER => array(
"authorization: Bearer sadhuUasjhda"
),
));
$result = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
print_r($result);exit;
Upvotes: 1