//Detect the request method
$method=$_SERVER['REQUEST_METHOD'];
//Url to the endpoint
$base_url_endpoint="URL";
//The service url detected as query_string uri part.
$endpoint_service=parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
//The payload for the request.
$request_body = file_get_contents('php://input');
$time_start = microtime(true);
error_log($time_start."-Proxy Request init for-".$request_body);
$ch = curl_init();
if (FALSE === $ch)
error_log($time_start."-Error on curl_init");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $base_url_endpoint.$endpoint_service);
curl_setopt($ch,CURLOPT_TIMEOUT,1000);
case "POST":
curl_setopt($ch, CURLOPT_POST, true);
if (isset($request_body))
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_body);
break;
case "PUT":
curl_setopt($ch, CURLOPT_PUT, 1);
break;
}
$respuesta= curl_exec($ch);
$info = curl_getinfo($ch);
if (isset($info["http_code"]) && $info["http_code"] == "500"){ //Error Case then die with an Exception
error_log($time_start."-Code 500");
http_response_code(500);
} else{
if (isset($respuesta) && $respuesta){
echo $respuesta;
error_log($time_start."-Code 400");
}
else{
error_log($time_start."-Error curl-".curl_error($ch));
}
}
curl_close($ch);
?>
Very important to mention
1.-The method request is detected and used for the response
2.-curl_setopt($ch,CURLOPT_TIMEOUT,1000); this option for the execution time in the server side.
3.-The header code in the response must be send as header response to the client.
No hay comentarios:
Publicar un comentario