1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| function requete_http($url, $variables)
{
//pour que la réponse s'affiche comme du texte brut
header('Content-Type: text/html');
$url = parse_url($url);
if(isset($url['port'])) {
$port = $url['port'];
} else {
$port = 8282;
}
//nom hote du site
$name = $url['host'];
//pour ne pas devoir calculer à la main la longueur du corps, on le stocke dans une variable et la fonction strlen() nous la donne.
$data = http_build_query($variables);
//la requête
$envoi = "POST ".$url['path']." HTTP/1.1\r\n";
$envoi .= "Host: ".$name."\r\n";
$envoi .= "Connection: Close\r\n";
$envoi .= "Content-type: application/x-www-form-urlencoded\r\n";
$envoi .= "Content-Length: ".strlen($data)."\r\n\r\n";
$envoi .= $data."\r\n";
//création du socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
//ouverture socket
@socket_connect($socket,gethostbyname($name), $port) OR die('texte qui affiche qu\'une exception est levée');
@socket_write($socket, $envoi, strlen($envoi)) OR die('texte qui affiche qu\'une exception est levée');
//lecture réponse
$reception = @socket_read($socket, 2024);
while(!preg_match('#^HTTP/1.1#', $reception)) {
$reception = socket_read($socket, 2024);
}
list($http, $reponse, $texte) = explode(" ", $reception, 3);
list($ext, $texte) = explode("Connection: close", $texte, 2);
socket_close($socket);
return array($reponse, $texte);
} |
Partager