I'm trying to make a PHP wrapper to manage web requests, but the code I wrote doesn't work at all. I always get an error when trying to connect (I'm using WAMP on my computer). I enabled allow_url_fopen, but it still doesn't work. Can someone help me? Here's the code:
<?php
class Wrapper {
var $timeout = 3000;
var $useragent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.8.1.8) Gecko/20071008 Firefox/2.0.0.8";
var $usegzip = true;
var $cookies;
function Get($url, $referer = '') {
$host = (substr($url, 0, 7) == 'http://') ? (substr($url, 7, (strpos($url, '/', 8) ? strpos($url, '/', 8) : strlen($url)) - 7)) : (substr($url, 0, strpos($url, '/', 0) ? strpos($url, '/', 0) : strlen($url)));
$location = substr($url, strpos($url, $host) + strlen($host), strlen($url) - strpos($url, $host));
if ($location == '') $location = '/';
$stream = @fsockopen($host, 80, $errnum, $errstring, $timeout);
if (!$stream) {
echo "Error #$errnum<br>\r\nConnection Failed: $errstring";
return false;
} else {
$headers = "GET $location HTTP/1.1\r\n";
$headers.= "Host: $host\r\n";
$headers.= "User-Agent: $useragent\r\n";
$headers.= "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n";
$headers.= "Accept-Language: en-gb,en;q=0.5\r\n";
$headers.= "Accept-Encoding: gzip,deflate\r\n";
$headers.= "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n";
$headers.= "Keep-Alive: 300\r\n";
$headers.= "Connection: keep-alive\r\n";
if ($referer != '') $headers.= "Referer: $referer\r\n";
$headers.= "Cookie: $cookies\r\n\r\n";
fputs($stream, $headers);
$buffer = '';
while (!feof($stream)) {
$buffer.= fgets($stream, 256);
}
return $buffer;
}
}
function Post($url, $postdata, $referer = '') {
$host = (substr($url, 0, 7) == 'http://') ? (substr($url, 7, (strpos($url, '/', 8) ? strpos($url, '/', 8) : strlen($url)) - 7)) : (substr($url, 0, strpos($url, '/', 0) ? strpos($url, '/', 0) : strlen($url)));
$location = substr($url, strpos($url, $host) + strlen($host), strlen($url) - strpos($url, $host));
if ($location == '') $location = '/';
$stream = @fsockopen($host, 80, $errnum, $errstring, $timeout);
if (!$stream) {
echo "Error #$errnum<br>\r\nConnection Failed: $errstring";
return false;
} else {
$headers = "POST $location HTTP/1.1\r\n";
$headers.= "Host: $host\r\n";
$headers.= "User-Agent: $useragent\r\n";
$headers.= "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n";
$headers.= "Accept-Language: en-gb,en;q=0.5\r\n";
$headers.= "Accept-Encoding: gzip,deflate\r\n";
$headers.= "Content-Length: ".strlen($postdata)."\r\n";
$headers.= "Content-Type: application/x-www-form-urlencoded\r\n";
$headers.= "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n";
$headers.= "Keep-Alive: 300\r\n";
$headers.= "Connection: keep-alive\r\n";
if ($referer != '') $headers.= "Referer: $referer\r\n";
$headers.= "Cookie: $cookies\r\n\r\n";
$headers.= $postdata;
fputs($stream, $headers);
$buffer = '';
while (!feof($stream)) {
$buffer.= fgets($stream, 256);
}
return $buffer;
}
}
}
$w = new Wrapper();
echo $w->Get("http://www.google.com/");
?>
I also don't know how to retrieve the Response headers, to handle the cookies. Could someone please help me?
ricky92