Bonjour bonjour,

Voila j'ai ecrit un petit script qui me permet d'envoyer une requete HTTP en PHP.

Tant que je le fait en GET j'ai pas de souci, par contre quand j'essaie en POST sa plante lamentablement puisque la page appellée ne retrouve pas les variable en POST et je ne capte pas bien pourquoi.

Pour le tester sans se prendre la tête télécharger ce fichier rar, dérarer le dans un rep sur votre serveur local et hop sa devrait fonctionner, ou presque !

http://ns30826.ovh.net/~clement/pers...%20requete.rar


Voila une copie du header type que je créé et envoie :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
 
POST /Copie%20de%20requete/back.php?unecle=unevleur HTTP/1.0
User-Agent : MSIE 1525
Host : MoiYOYO
Content-type : application/x-www-form-urlencoded
Content-length : 15
 
unecle=unevleur

Au passage si il y'avait un guru de l'expression régulière qui veux bien m'aidé à créer une RegExp pour séparer l'entete du contenu je n'en serait pas mécontent !
Parce qu'il faut bien dire que pour le moment c'est pas terrible :s

Sa se passe à cet endroit :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
 
      /* HTTP BODY */ 
      /* Separation de l'entete du contenu */ 
      $temp = $status; 
      $temp .= implode("\n",$entete); 
 
      $this->HTTPResponse->Headers = $temp; 
      $this->HTTPResponse->Body = substr($content,strlen($temp));

Merci pour votre aide !

Bonne journée


Le script complet le voila :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
 
<?
 
class HTTPRequest
{
	/* PRIVEE */
	private $Socket;				// Socket de connexion au serveur
	private $PORT;					// Numéro du port sur lequel se connecter
	private $HostToCall;			// Domaine ou ip à appeler
	private $HTTPResponse;			// Domaine ou ip à appeler
 
	public function __construct ( )
	{
		$this->Socket 				= "";
		$this->PORT 				= 80;
		$this->HostToCall			= "";
	}
 
	public function ExecuteSSLRequest( HTTPHeaders $Header , $HostToCall )
	{
		/* Fonctionne uniquement si l'appel du script est réalisé en SSL */
		$query = $errno = $errstr = $content = "";
		$this->PORT = 443;
		$this->HTTPResponse = NULL;
 
		$this->HostToCall 	= $HostToCall;
		$this->Socket 		= fsockopen("ssl://".$this->HostToCall , $this->PORT , $errno , $errstr , 10 );
 
		try{
			$this->StartHTTPRequest( $Header );
		}catch(Exception $E){
			throw new Exception ( $E );
		}
 
		try{
			$this->ReadHTTPResponse ( );
		}catch(Exception $E){
			throw new Exception ( $E );
		}
 
		fclose($this->Socket);
		return $this->HTTPResponse;
 
	}
 
	public function ExecuteRequest (HTTPHeaders $Header , $HostToCall )
	{
		$query = $errno = $errstr = $content = "";
		$this->PORT = 80;
		$this->HTTPResponse = NULL;
 
		$this->HostToCall 	= $HostToCall;
		$this->Socket 		= fsockopen($this->HostToCall , $this->PORT , $errno , $errstr , 10 );
 
		try{
			$this->StartHTTPRequest( $Header );
		}catch(Exception $E){
			throw new Exception ( $E );
		}
 
		try{
			$this->ReadHTTPResponse ( );
		}catch(Exception $E){
			throw new Exception ( $E );
		}
 
		fclose($this->Socket);
		return $this->HTTPResponse;
	}
 
	private function StartHTTPRequest( $Header )
	{
		/* L'ouverture du socket c'est il bien déroulé ? si non, pourquoi */
		if(!$this->Socket)
		{
			fclose($this->Socket);
			throw new Exception ( $errno." ".$errstr );
		}
 
		/* Envoi du HEADER HTTP  */
		if( !fputs($this->Socket , $Header->__tostring()) )
		{
			fclose($this->Socket);
			throw new Exception ( "Impossible d'ennvoyer le HEADER HTTP !" );
		}
		//echo $Header->__tostring();
	}
 
	private function ReadHTTPResponse ( )
	{
		$this->HTTPResponse = new HTTPResponse();
 
		$content 	 = "";
		$status 	 = "";
		$entete 	 = "";
		$contenu 	 = "";
		$regs		 = array();
		$CRLF		 = chr(13).chr(10);	/* CARRIAGE RETURN LINE FEED */
		$CRLF		 = "\x0d\x0a";
		$CRLF		 = "\r\n";
		while( !feof($this->Socket) )
		{
			$content .= fgets($this->Socket);
		}
 
		/* Hypertext Transfer Protocol -- HTTP/1.1 -  RFC 2616 - http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html  */
 
		/* HTTP STATUS */
		$pattern 	= '#^HTTP/[0-9][.][0-9][ ][0-9]{3}[ ]{1}[\w- ]*['.$CRLF.']$#m';
		$regs 		= array();
		if(preg_match_all ( $pattern , $content , $regs) )
		{
			$status = $regs[0][0];
		}
		else
		{
			throw new Exception( "HEADER STATUS introuvable !" );
		}		
 
		/* HTTP INFO */
		$pattern 	= "#^([A-Z-a-z]{1,}[\s]{0,1}[:]{1}[\s]{0,1})[0-9-\w-,-:- -\\-/-_]{1,}[".$CRLF."]$#m";
		$regs 		= array();
		if(preg_match_all ( $pattern , $content , $regs) )
		{
			$entete = $regs[0];
		}
		else
		{
			throw new Exception( "HEADER HTTP introuvable !" );
		}
		/* HTTP VARS */
		//$pattern = "^([a-z-A-Z]*[\s]?[:]{1}[\s]?)[\w-,-:- -\\-/-_]*$[\r\n]{1}";
 
		/* HTTP BODY */
		/* Separation de l'entete du contenu */
		$temp = $status;
		$temp .= implode("\n",$entete);
 
		$this->HTTPResponse->Headers = $temp;
		$this->HTTPResponse->Body = substr($content,strlen($temp));
	}
}
 
class HTTPResponse
{
	public $Headers;
	public $Body;
 
	public function __construct()
	{
		$this->Headers 	= array();
		$this->Body 	= "";
	}
 
	public function __toString()
	{
		return $this->Body;
	}
}
class HTTPHeaders
{
 
	public $UserAgent;			// Nom du navigateur qui appelle la page	'MSIE'
	public $Host;				// nom de domaine appelant 					'www.monsite.com' - 213.21.25.26
	public $ContentType;		// Type mime du contenu envoyer 			'text/xml'
	public $Version;			// Version du protocole						1.0
	public $URL;				// Chemin d'accès à la page					'/webservices/yo/'
	public $TYPE;				// Type de requete à effectuer 				'GET' ou 'POST'
	public $GetVars;
 
	public function __construct ( )
	{
		$this->UserAgent 		= "";
		$this->Host 			= "";
		$this->ContentType 		= "";
		$this->Version 			= "1.0";
		$this->URL 				= "";
		$this->TYPE 			= "";
		$this->GetVars 			= new GetVars();
		$this->PostVars 		= new PostVars();
	}
 
	public function __tostring()
	{
		$strReturn = "";
		$strReturn .= $this->TYPE." ".$this->RawUrlEncode ($this->URL);
		if($this->GetVars->__toString() != ""){
		$strReturn .= "?".$this->GetVars->__toString();
		}
		$strReturn .= " HTTP/".$this->Version."\r\n";
		$strReturn .= "User-Agent : ".$this->UserAgent."\r\n";
		$strReturn .= "Host : ".$this->Host."\r\n";
		$strReturn .= "Content-type : ".$this->ContentType."\r\n";
		$strReturn .= "Content-length : ".strlen($this->PostVars->__toString());
		$strReturn .= "\r\n\r\n".$this->PostVars->__toString()."\r\n";
		return $strReturn;
	}
 
	private function RawUrlEncode( $url ) {
	   $parts = explode('/', $url);
	   for ($i = 0; $i < count($parts); $i++)
	   {
		 $parts[$i] = rawurlencode($parts[$i]);
	   }
	   return implode('/', $parts);
	}
}
 
class GetVars
{
 
	public $TabVars;
 
	public function __construct ()
	{
		$this->TabVars = array();
	}
 
	public function add($Key , $Value)
	{
		$this->TabVars[$Key] = $Value;
	}
 
	public function __toString()
	{
		$strReturn = "";
		foreach($this->TabVars as $Key => $Value)
		{
			$strReturn .= urlencode($Key)."=".urlencode($Value)."&";
		}
		$strReturn = substr($strReturn , 0 , strlen($strReturn)-1 );
		return $strReturn;
	}
}
 
class PostVars
{
	public $TabVars;
 
	public function __construct ()
	{
		$this->TabVars = array();
	}
 
	public function add($Key , $Value)
	{
		$this->TabVars[$Key] = $Value;
	}
 
	public function __toString()
	{
		$strReturn = "";
		foreach($this->TabVars as $Key => $Value)
		{
			$strReturn .= $Key."=".$Value."&";
		}
		$strReturn = substr($strReturn , 0 , strlen($strReturn)-1 );
		return $strReturn;
	}
}
 
?>