
zcriptz
1
Ómicron
Programador
Verificación en dos pasos activada
Verificado por Whatsapp
Suscripción a IA
Guardan este código en un archivo PHP con el nombre que quieran, en este caso SOCKET.php
En el caso del demo estaría devolviendo el código HTML de ForoBeta, enviando como referer "prueba.com".
Puse algunas cabeceras para que vean como se pueden agregar.
Para enviar campos post, deben enviarlos como tercer parametro en la función "browse", con este formato: campo=valor&campo2=valor&otrocampo=loquesea
Código DEMO:
Demo: SOCKET
Pueden colocar esto al final para comprender el funcionamiento:
____________________________
Clase para navegar webs con cURL (PHP)
PHP:
<?php
class SOCKET{
public $connection = 'close';
public $method = 'GET';
public $ports = array(
'http' => 80,
'ssl' => 443
);
public function __construct($cookies=NULL, $agent=NULL){
$this->cookiesJar = $cookies;
$this->agent = $agent;
}
public function browse($URL=NULL, $referer=NULL, $post=NULL){
if($URL) $this->URL = $URL;
if($referer) $this->referer = $referer;
if($post) $this->post = $post;
$this->makeData()->send();
return $this;
}
public function makeData(){
$this->URL = str_ireplace('https', 'ssl', $this->URL);
$n = "\r\n";
$this->urlData = parse_url($this->URL);
if(!$this->urlData['path']) $this->urlData['path'] = '/';
if($this->post AND !$this->method) $this->method = 'POST';
$this->data = $this->method.' '.$this->urlData['path'].' HTTP/1.1';
$this->data .= $n.'Host: '.$this->urlData['host'];
$this->data .= $n.'User-Agent: '.$this->agent;
if($this->headers) foreach($this->headers AS $header) $this->data .= $n.$header;
if($this->cookiesJar AND file_exists($this->cookiesJar)){
$this->allCookies = file_get_contents($this->cookiesJar);
if($this->cookies = preg_match('/'.$this->urlData['host'].'\{([^\}]+)\}/is', $this->allCookies, $cMath) ? array_filter(explode("\r\n", $cMath[1])) : NULL){
$this->data .= $n.'cookie: ';
foreach($this->cookies AS $cookie){
$cookieData = explode(':', $cookie);
$this->cookiesArray[trim(explode('=', $cookieData[1])[0])] = trim($cookie);
$this->data .= explode(';', trim($cookieData[1]))[0].'; ';
}
}
$this->data = trim(trim($this->data), ';').$n;
}
$this->data .= 'Connection: '.$this->connection;
if($this->post) $this->data .= $n.$this->post;
$this->data .= $n.$n;
return $this;
}
public function send(){
if(!$this->port) $this->port = $this->ports[$this->urlData['scheme']];
$this->socket = fsockopen($this->urlData['scheme'].'://'.$this->urlData['host'], $this->port, $errno, $this->error, 300);
fwrite($this->socket, $this->data);
stream_set_timeout($this->socket, 2);
$this->response = '';
while(!feof($this->socket)) $this->response .= fgets($this->socket, strlen($this->data));
fclose($this->socket);
$this->parse($this->response);
return $this;
}
public function parse($response){
if($response) $this->response = $response;
list($this->resHeaders, $this->result) = preg_split("/\R\R/", $this->response, 2);
$this->resHeaders = explode("\r\n", $this->resHeaders);
foreach(preg_grep('/set-cookie:*/si', $this->resHeaders) AS $cookie){
$cookieData = explode(':', $cookie);
$this->cookiesArray[trim(explode('=', $cookieData[1])[0])] = $cookie;
}
if($this->cookiesArray) foreach($this->cookiesArray AS $cookie) $cookiesText .= " ".$cookie."\r\n";
if(!stristr($this->allCookies, $this->urlData['host'].'{')) $this->allCookies .= $this->urlData['host']."{\r\nprueba}\r\n";
$this->allCookies = ($this->cookiesArray) ? preg_replace('/'.$this->urlData['host'].'\{([^\}]+)\}/is', $this->urlData['host']."{\r\n$cookiesText}\r\n", $this->allCookies) : $this->urlData['host']."{\r\n".$cookiesText."}";
file_put_contents($this->cookiesJar, $this->allCookies);
if($location = array_shift(preg_grep('/location:*/si', $this->resHeaders))){
$this->browse(trim(str_ireplace('location:', '', $location)));
}
return $this;
}
}
?>
En el caso del demo estaría devolviendo el código HTML de ForoBeta, enviando como referer "prueba.com".
Puse algunas cabeceras para que vean como se pueden agregar.
Para enviar campos post, deben enviarlos como tercer parametro en la función "browse", con este formato: campo=valor&campo2=valor&otrocampo=loquesea
Código DEMO:
PHP:
<?php
require_once('SOCKET.php');
$SOCKET = new SOCKET('cookies.txt', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36');
$SOCKET->headers = array(
'accept: */*',
'origin: https://www.forobeta.com',
'accept-language: es-ES,es;q=0.9'
);
$SOCKET->browse('https://www.forobeta.com', 'https://www.prueba.com');
echo '<b>Código HTML de ForoBeta:</b><br><textarea style="width:500px;height:100px">'.$SOCKET->result.'</textarea><br><br>';
?>
Demo: SOCKET
Pueden colocar esto al final para comprender el funcionamiento:
PHP:
<b>Socket enviado:</b><br>
<textarea style="width:500px;height:100px"><?=$SOCKET->data?></textarea><br>
<br>
<b>Respuesta de <?=$SOCKET->URL?>:</b><br>
<textarea style="width:500px;height:100px"><?=$SOCKET->response?></textarea><br>
<br>
<b>Cabeceras recibidas:</b><br>
<textarea style="width:500px;height:100px"><? print_r($SOCKET->resHeaders); ?></textarea><br>
<br>
<b>Cuerpo recibido:</b><br>
<textarea style="width:500px;height:100px"><?=$SOCKET->result?></textarea><br><br>
Clase para navegar webs con cURL (PHP)
Última edición: