[API Google] Acortar URL ??, no me retorna !!

Iniciado por Diabliyo, 24 Enero 2012, 00:14 AM

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

Diabliyo

Buen dia.

Estoy intentand acortar URLs con mi codigo PHP usando el servicio de goo.gl, pero no me retorna la url acortada :(. Esto es lo que hago:

Código (php) [Seleccionar]
<?php
$url'http://miurl.com/bien/largaaaaaaaaa.html'# url larga 
$api_key='123456789asdasdsadasdasdasd'# mi api
$host'www.googleapis.com'# servidor
$port'443'# puerto ssl
$path'/urlshortener/v1/url?'# path donde se hara el request POST
$buf= array( "longUrl"=>urlencode($url) ); # array para pasar a json
$data= array( 'POST'$path.$api_keyjson_encode($buf) ); # pasando a json

$r='';

$http_request  "$data[0] $data[1] HTTP/1.0\r\n";
$http_request .= "Host: $host\r\n";
$http_request .= "Content-Type: application/json;\r\n";
$http_request .= "\r\n";
$http_request .= $data[2];

if( ($fs = @fsockopen($host$port$errno$errstr10))==FALSE )
echo 'No se puede abrir socket :: ['$errno'] '$errstr;
else
{
fwrite($fs$http_request);

while ( !feof($fs) )
$r .= fgets($fs1160); // One TCP-IP packet
fclose($fs);
$rexplode("\r\n\r\n"$r2);

# exito
print_r($r);
}
?>


Y retorna:

Array ( [0] =>

~ Yoya ~

El código esta bien excepto por 3 problemas que nunca te darías cuenta ya que desactivaste la opción de mostrar error en la linea donde inicia la instancia del socket... Gran error...

Aqui te listo los errores:

  • Es necesario enviar la longitud del contenido via POST, utilizando Content-length: - Requisito para utilizar la api de goo.gl
  • Estas usando urlencode para aplicarlo a la url que en este caso no es debido ya que estas estableciendo que el contenido es en json
  • En ningún momento agregaste el protocolo ssl aunque si el puerto.

Código (php,7,18,22) [Seleccionar]
<?php

$url'http://miurl.com/bien/largaaaaaaaaa.html'# url larga 
$host'www.googleapis.com'# servidor
$port'443'# puerto ssl
$path'/urlshortener/v1/url'# path donde se hara el request POST
$buf= array( "longUrl"=>$url); # array para pasar a json
$data= array( 'POST'$pathjson_encode($buf) ); # pasando a json

$r='';

$http_request  "$data[0] $data[1] HTTP/1.1\r\n";
$http_request .= "Host: $host\r\n";
$http_request .= "Content-Type: application/json;\r\n";

//Necesario enviar la longitud de contenido post a enviar
//Es un requisito para poder usar la api de goo.gl
$http_request .= "Content-length: ".strlen(json_encode($buf))."\r\n";
 
$http_request .= "Connection: close\r\n\r\n";
$http_request .= $data[2]."\r\n";

if( ($fs fsockopen('ssl://'.$host$port$errno$errstr10))==FALSE )
echo 'No se puede abrir socket :: ['$errno'] '$errstr;
else
{
fwrite($fs$http_request);
 
while ( !feof($fs) )
$r .= fgets($fs1160); // One TCP-IP packet
fclose($fs);
$rexplode("\r\n\r\n"$r2);
 
# exito
print_r($r);
}
?>




Array
(
   [0] => HTTP/1.1 200 OK
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Date: Tue, 24 Jan 2012 15:00:53 GMT
ETag: "x_mUziAiWO8znp8PiIztCZO6CzY/tFCfIhMbNBGL2qIFNmRkmEaCOiE"
Content-Type: application/json; charset=UTF-8
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Connection: close
   [1] => {
"kind": "urlshortener#url",
"id": "http://goo.gl/iqnUe",
"longUrl": "http://miurl.com/bien/largaaaaaaaaa.html"
}

)



Saludos.
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.