obtener enlace HTTP con preg_match ??

Iniciado por Diabliyo, 18 Febrero 2011, 22:32 PM

0 Miembros y 1 Visitante están viendo este tema.

Diabliyo

Buenas..

Hice un scrtip para acortar mis URLs en mi blog, el proceso lo hago muy simple:

1- me conecto al sitio is.gd con sockets.
3- Envio la peticion GET con mi URL.
4- Me responde con la URL acortada.

Mi problema, es que en lo que es la trama HTTP me llegan otros valores extras, esto me llega:

1- Url a acortar: http://lab.sie-group.net.
2- GET que invoco en el socket: http://is.gd/create.php?format=simple&url=http://lab.sie-group.net
3- Respuesta a nivel de socket: 13 http://is.gd/DuSqfL 0.

Si lo hacemos via Navegador, recivo solo: http://is.gd/DuSqfL, el valor 12 y 0 no se porque estan en la trama, en fin que puedo hacer para sacar la url ?

Este es mi code:

Código (php) [Seleccionar]
<?php
function is_gd( $url )
{
$host= 'is.gd'; # hostname
$get= 'create.php?format=simple'; # get basico  
$url= urlencode($url); # acondicionamos url 
$get .= '&url='. $url; # concatenamos la url
$puerto= 80; # puerto a consultar 

# trama HTTP 
$http_request= "GET /$get HTTP/1.1\r\n";
$http_request .= "Host: $host \r\n";
$http_request .= "Connection: close\r\n";
$http_request .= "\r\n";

# consultamos 
if( ($fd= @fsockopen($host, $puerto, $errno, $errstr, 10 ))==FALSE ) # abrimos socket
return 0; # error, no se abrio el socket

fwrite($fd, $http_request); # enviamos datos
$buf=''; # buffer de recepcion 

while( !feof($fd) ) # leeremos sockets hasta que termine  
$buf .= fgets($fd, 2048); # leemos
fclose($fd); # cerramos flujo  

$estado='';
preg_match("{[0-9]{3}}", $buf, $a); # obtenemos codigo recivido en transferencia
$estado= $a[0]; # copiamos estado 
unset($a); 

$failstatus= array( "400", "406", "502", "503" ); # codigos de error en consulta 
foreach( $failstatus as $key )
{
if( !strcmp($key, $estado ) ) # si son iguales
echo 'Error en consulta: '. $estado;
# return 0; # error en la consulta
}

$r= explode( "\r\n\r\n", $buf );

echo '<br>Respuesta: '. $r[1];
}
?>


Se imprime:

Respuesta: 13 http://is.gd/DuSqfL 0

Saludos !

~ Yoya ~

#1
Código (php) [Seleccionar]
<?php
function is_gd( $url )
{
$host= 'is.gd'; # hostname
$get= 'create.php?format=simple'; # get basico  
$url= urlencode($url); # acondicionamos url 
$get .= '&url='. $url; # concatenamos la url
$puerto= 80; # puerto a consultar 

# trama HTTP 
$http_request= "GET /$get HTTP/1.1\r\n";
$http_request .= "Host: $host \r\n";
$http_request .= "Connection: close\r\n";
$http_request .= "\r\n";
 
# consultamos 
if( ($fd= @fsockopen($host, $puerto, $errno, $errstr, 10 ))==FALSE ) # abrimos socket
return 0; # error, no se abrio el socket

fwrite($fd, $http_request); # enviamos datos
$buf=''; # buffer de recepcion 

while( !feof($fd) ) # leeremos sockets hasta que termine  
$buf .= fgets($fd, 2048); # leemos
fclose($fd); # cerramos flujo  

        
preg_match("/13\r\n([^\r\n]+)/", $buf, $result);
        echo 
$result[0];
}

is_gd('http://lab.sie-group.net/');
?>


Salida:
http://is.gd/q9rWNd
Mi madre me dijo que estoy destinado a ser pobre toda la vida.
Engineering is the art of balancing the benefits and drawbacks of any approach.