由于我的 api 请求服务器中的此 504 错误,我遇到了麻烦。我正在使用此代码获取 api 数据
$api_url="*****";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$parsed_json = curl_exec($ch);
$parsed_json = json_decode($parsed_json);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //check if 504 return.
有时我会检查代码是否返回 200 作为正常响应。
你可以这样做
$api_url="****";
$t = microtime( TRUE );
file_get_contents($api_url);
$t = microtime( TRUE ) - $t;
if($t<=5){
Execute curl here
}