I´m using the following code to fetch content from a webiste.
Unfortunatelly, I can make it work.
The website I´m trying to read info from seems to redirect me twice; I can get to the desired page, but got "junk" data insted of the actual content.
Could anyone please help me??
Thank you.
Code: Select all
<?php
class GetWebObject {
var $host = "";
var $port = "";
var $path = "";
var $header = array();
var $content = "";
var $redirect = "";
var $status = "";
function GetWebObject($host, $port, $path)
{
$this->host = $host;
$this->port = $port;
$this->path = $path;
$this->content = "";
$this->status = "x";
while (strpos($this->status,"HTTP/1.0 200 OK") === false and $this->status != '') {
$this->fetch();
}
}
function fetch()
{
$this->redirect = "";
$this->content = "";
$fp = fsockopen ($this->host, $this->port);
if(!$fp) {
die("No puede conectarse con Mercado Libre.");}
$header_done=false;
$request = "GET ".$this->path." HTTP/1.0\r\n";
$request .= "User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)\r\n";
$request .= "Host: ".$this->host."\r\n";
$request .= "Connection: close\r\n\r\n";
fputs ($fp, $request);
$line = fgets ($fp, 128);
$this->status = $this->headerї"status"] = $line;
while (!feof($fp)) {
if($header_done) {
$linea = fread ( $fp, 1024 );
if (strpos($linea,'The URL has moved <a href="') == 0) {
$this->headerї"Location"] = substr($linea,strpos($linea,'The URL has moved <a href="')+strlen('The URL has moved <a href="'),(strpos($linea,'">')-strpos($linea,'The URL has moved <a href="')));
$this->headerї"Location"] = substr($this->headerї"Location"], 0, strpos($this->headerї"Location"], '">'));
break;
} else {
$this->content .= $linea;
}
} else {
$line = fgets ($fp, 128);
if($line == "\r\n") {
$header_done=true;}
else {
$data = explode(": ",$line);
$this->headerї$dataї0]] = $dataї1];
}
}
}
fclose ($fp);
$this->path = $this->headerї"Location"];
}
}
$url = $_GETї'url']; // Trailing slash when not using filename
$url = 'http://www.mercadolibre.com.ar/jm/pms?site=304970&id=2021&as_opt=http://www.mercadolibre.com.ar/jm/item?site=MLA$$id=15502430';
//'http://www.mercadolibre.com.ar/jm/item?site=MLA&id=15502430';
//'http://www.mercadolibre.com.ar/jm/pms?site=304970&id=2021&as_opt=http://www.mercadolibre.com.ar/jm/item?site=MLA$$id=15502430';
$url = substr($url,7);
$host = trim(substr($url,0,strpos($url,'/')));
$path = trim(substr($url,strpos($url,'/')));
$cont = 0;
$file = new GetWebObject($host, 80, $path);
print $file->status;
$lineas = split("\n",$file->content);
for ($i=0; $i<=count($lineas); $i++) {
$buffer = $lineasї$i];
if (strpos($buffer,'comprar y vender en MercadoLibre')) {
$buffer = str_replace('comprar y vender en MercadoLibre','comprar en www.ringtones.com.ar',$buffer);
}
$texto = 'El uso de este sitio';
if (strpos($buffer,$texto)) {
$buffer = str_replace($texto,'<a href=http://www.ringtones.com.ar target=_blank >Ringtones</a><br><br>'.$texto,$buffer);
}
$texto = '/jm/reg';
if (strpos($buffer,$texto)) {
$buffer = str_replace($texto,'http://www.mercadolibre.com.ar/argentina/ml/pms?site=304970&id=2021&go=RG',$buffer);
}
$texto = 'href=/';
if (strpos($buffer,$texto)) {
$buffer = str_replace($texto,'href=http://www.ringtones.com.ar/links.php?url='.urlencode('http://www.mercadolibre.com.ar/'),$buffer);
}
print $buffer;
}
?>Code: Select all
TAGS.Code: Select all
tags coming soon to a theatre near you[/color][/size]