<?php
function curl($url, $post_data="", $headers="", $cookie_file=""){
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
if(!empty($headers)){curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);}
if(!empty($post_data)){
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
}
if(!empty($cookie_file)){
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file);
}
$response = curl_exec($curl);
if(curl_error($curl)){
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$error = "Error = " . curl_error($curl) . " - " . 'HTTP code: ' . $httpcode;
curl_close($curl);
return array(false, $error);
}
curl_close($curl);
return (empty($response) ? array(false, "No hay nada de respuesta.") : array(true, $response));
}
$response = curl("http://localhost/post_test/archivo.php","key=12345");
if($response[0] === true){
echo $response[1]; // Aca se podria validar por ejemplo si la respuesta contiene cierta palabra por ejemplo Acceso permitido y así saber que todo es OK
}else{
echo "Error en la peticion: " . $response[1];
}
?>