Código:
También he probado con esta otra:<?php
//conecto con la base de datos
$Servidor = "localhost";
$usuario = "usuario";
$clave = "cpassword";
$bbdd = "bbdd";
$connectid = mysql_connect($Servidor, $usuario, $clave);
mysql_select_db($bbdd);
$ssql = "select * from Bocm order by Fecha desc";
$result = mysql_query($ssql);
while ($registro = mysql_fetch_array($result))
{
$mysqli = new mysqli("localhost", "bfxbdf", "dfbdfbd", dfbdbs");
if (mysqli_connect_errno()) {
echo "Error enconexión: ". mysqli_connect_error();
exit();}
//_________________________________________________
$url= $registro["Enlace"]; # url larga
$api_key=$key; # 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_key, json_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, $errstr, 10))==FALSE )
echo 'No se puede abrir socket :: ['. $errno. '] '. $errstr;
else
{
fwrite($fs, $http_request);
while ( !feof($fs) )
$Enlcort .= fgets($fs, 1160); // One TCP-IP packet
fclose($fs);
$r= explode("\r\n\r\n", $r, 2);
# exito
print_r($Enlcort);
}
//_________________________________________________
//if ($Enlcort != $registro["Enlacecorto"]){
$sql ="UPDATE Bocm SET Enlacecorto = '" . $Enlcort . "' where Enlace = '" . $registro["Enlace"] . "'";
/* fetch array asociativo*/
if ($rs = $mysqli->query($sql)) {
echo "Dentro";
/*while ($fila = $rs->fetch_assoc()) {
}*/
}
//}
echo "Dirección: " . $registro["Enlace"] . " " . $registro["Bocm ID"] . " Dirección corta: " . $Enlcort . "/n";
}
/* liberamos la memoria asociada al resultado */
$rs->close();
/* cerramos la conexion */
$mysqli->close();
?>
Código:
<?php
//Acortador URL Google
//___________________________________________
// Declarar la clase
class GoogleUrlApi {
// Constructor
function GoogleURLAPI($key,$apiURL = 'https://www.googleapis.com/urlshortener/v1/url') {
// Keep the API Url
$this->apiURL = $apiURL.'?key='.$key;
}
// Acortar una URL
function shorten($url) {
// Send information along
$response = $this->send($url);
// Return the result
return isset($response['id']) ? $response['id'] : false;
}
// Expandir una URL
function expand($url) {
// Envía esta información
$response = $this->send($url,false);
// Vuelve a los resultados
return isset($response['longUrl']) ? $response['longUrl'] : false;
}
// Enviar informacion a Google
function send($url,$shorten = true) {
// Create cURL
$ch = curl_init();
// SI estamos acortado una URL...
if($shorten) {
curl_setopt($ch,CURLOPT_URL,$this->apiURL);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode(array("longUrl"=>$url)));
curl_setopt($ch,CURLOPT_HTTPHEADER,array("Content-Type: application/json"));
}
else {
curl_setopt($ch,CURLOPT_URL,$this->apiURL.'&shortUrl='.$url);
}
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
// Ejecutar el post
$result = curl_exec($ch);
// Cerrar la conexión
curl_close($ch);
// Devolver el resultado
return json_decode($result,true);
}
}
//___________________________________________
//Ahora creemos una instancia de GoogleUrlApi para acortar o expandir una URL:
// Crear instancia con una clave
$key = 'AIzaSyA530eUn-c-jc_jevu-1HUyq1f35x90lPA';
$googer = new GoogleURLAPI($key);
//conecto con la base de datos
$Servidor = "localhost";
$usuario = "boletines";
$clave = "Ubibos7582";
$bbdd = "boletines";
$connectid = mysql_connect($Servidor, $usuario, $clave);
mysql_select_db($bbdd);
echo " 2";
//sentencia SQL para acceder a los últimos 10 artículos publicados
$ssql = "select * from Bocm order by Fecha desc";
$result = mysql_query($ssql);
while ($registro = mysql_fetch_array($result))
{
/* Relleno campo Sumario*/
$mysqli = new mysqli("localhost", "boletines", "Ubibos7582", "boletines");
if (mysqli_connect_errno()) {
// echo "Error enconexión: ". mysqli_connect_error();
exit();}
$Enlcort= $googer->shorten($registro["Enlace"]);
//if ($Enlcort != $registro["Enlacecorto"]){
$sql ="UPDATE Bocm SET Enlacecorto = '" . $Enlcort . "' where Enlace = '" . $registro["Enlace"] . "'";
/* fetch array asociativo*/
if ($rs = $mysqli->query($sql)) {
echo "Dentro";
/*while ($fila = $rs->fetch_assoc()) {
}*/
}
//}
echo "Dirección: " . $registro["Enlace"] . " " . $registro["Bocm ID"] . " Dirección corta: " . $Enlcort . "/n";
}
/* liberamos la memoria asociada al resultado */
$rs->close();
/* cerramos la conexion */
$mysqli->close();
?>
Muchas gracias y saludos cordiales.


