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
| <?php
/**
* Pateysoft Creation
* All rights reserved
* 2007-2008
* @author Ludovic PATEY
* @version 1.0
*
* This is a class used to communicate with others serveurs trough the PostMethod.
* I had to developp this class in order to communicate with campus-booster
*/
class PostRequest {
private $url;
private $cookies = array();
private $meta = array();
private $data = array();
private $headers = array();
public function __construct( $url ) {
$this->url = $url;
$this->setHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
}
public function setCookies( $cookies ) {
$this->setHeader( 'cookie', $this->buildCookies( $cookies ) );
$this->cookies = $cookies;
}
public function setData( $name, $value ) {
$this->data[ $name ] = $value;
}
public function getCookies() {
return $this->cookies;
}
public function setHeader( $name, $value ) {
$this->headers[ $name ] = $value;
}
public function send() {
$headers = '';
foreach( $this->headers as $name => $value ) {
$headers .= $name . ': ' . $value . "\r\n";
}
$content = http_build_query( $this->data );
$headers.= 'Content-Length: ' . strlen( $content );
$context = stream_context_create(
array( 'http' => array( 'user_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1) Gecko/20061010 Firefox/2.0',
'method' => 'POST',
'content' => $content,
'header' => $headers ) ) );
$fh = fopen( $this->url, 'r', false, $context );
$this->meta = stream_get_meta_data( $fh );
$cookies = array();
foreach( $this->meta['wrapper_data'] as $data ) {
if( preg_match( '/Set-Cookie: ([^=]+)=([^;]+)/', $data, $cookie ) ) {
$cookies[ $cookie[1] ] = $cookie[2];
}
}
$this->cookies = $cookies;
$return = '';
while( !feof( $fh ) ) {
$return .= fread( $fh, 1024 );
}
fclose( $fh );
return $return;
}
private function buildCookies( $cookies ) {
$return = '';
foreach( $cookies as $name => $value ) {
$return .= ' ' . $name . '=' . $value . ';';
}
return trim($return);
}
}
?> |
Partager