I try to use PHP CURL to call REST API(API URL is good for I can access josn data from browser), the Codes are:
$url = 'https://mobilesamples.bentley.com/ws/v1.2/DataSources';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$ret = curl_exec($ch);
if($ret == false)
{
var_dump(curl_error($ch));
}
$retinfo = curl_getinfo($ch);
curl_close($ch);
if($retinfo['http_code']==200)
{
$data = json_decode($ret, true);
print_r($data);
}
I find Http_code = 301, it means redirection happens so I add this:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
But I get Http_code = 404 and pages not found, it redirects to an invalid pag: https://mobilesamples.bentley.com/wsv1/v1.2/DataSources
How can I get right json data through curl just as opened from browser?