Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

andrenx

URL Curta goo.gl Dinâmicas

Recommended Posts

Olá, Pessoal.

 

Espero que ajudo os demais membros do forum, Tive ajuda de uma pessoa que para mim sempre foi grande parceiro, Beraldo, ele que me ajudo a concluir.

 

Qual proposito do script, pega URL do seu navegador deixar ela curta usado classe do Googl.go

 

Créditos:

Beraldo - Revisão e correção do script.

Classe - https://github.com/moacirmoda/googl

 

 

googl.class.php

<?php

/**
* Googl
*
* @author Eslam Mahmoud
* @url http://eslam.me/
* @copyright Creative Commons Attribution-ShareAlike 3.0 Unported License.
* @version 0.2
* @access public
*/
class Googl {
   //application key
   private $APIKey;
   //api url
   private $API = "https://www.googleapis.com/urlshortener/v1/url"; 

   // short_url
   public $short_url;

 /**
  * Googl::Googl()
  *
  * @param string $apiKey
  * @return void
  */
   function Googl($apiKey=""){
       if ($apiKey != ""){
           $this->APIKey = $apiKey;
       }
   } 

 /**
  * Googl::get_long()
  *
  * @param url as string $shortURL
  * @return result as array
  */
   function get_long($shortURL , $analytics = false){
       $url = $this->API.'?shortUrl='.$shortURL; 

       if ($this->APIKey){
           $url .= '&key='.$this->APIKey;
       }
       if ($analytics){
           $url .= '&projection=FULL';
       }
       $ch = curl_init($url);
       curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       $result=curl_exec($ch);
       curl_close($ch);
       $array = json_decode($result, true);
       return $array;
   } 

 /**
  * Googl::set_short()
  *
  * @param url as string $longURL
  * @return result as array
  */
   function set_short($longURL){
       $vars = "";
       if ($this->APIKey){
           $vars .= "?key=$this->APIKey";
       } 

       $ch = curl_init($this->API.$vars);
       curl_setopt($ch, CURLOPT_POST, 1);
       curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: application/json'));
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false );
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       curl_setopt($ch, CURLOPT_POSTFIELDS, '{"longUrl": "' . $longURL . '"}');
       $result=curl_exec($ch);
       curl_close($ch);
       $array = json_decode($result, true);
       $this->short_url = $array['id'];

       return $array;
   }

  /**
  * Googl::get_short()
  *
  * @param 
  * @return attribute short_url
  */
   function get_short(){
     return $this->short_url;
   }
} 

?>

 

mostrar.php

<?php
require "googl.class.php";

//Função Pega URL ATUAL
function UrlAtual(){
$dominio= $_SERVER['HTTP_HOST'];
$url = "http://" . $dominio. $_SERVER['REQUEST_URI'];
return $url;
}

$googl = new Googl("SUA CHAVE");
$googl->set_short( UrlAtual() );
print $googl->get_short();
//MOSTRAR URL CURTA
print_r($r);
?>

Compartilhar este post


Link para o post
Compartilhar em outros sites

Aproveitando a deixa de uso da própria API do Google, fica uma solução simplificada, isto é, sem verificações nem API Auth Key, sobre como implementar em PHP com Stream Contexts.

 

<?php

/**
* Google URL Shortener usando Google API e Stream Context
*
* @param  mixed|string $url
*
* @return string
*/
function GoogleUrlShortener( $url ) {

   $opts = array(

       'http' => array(

           'method'  => 'POST',
           'header'  => 'Content-Type: application/json',
           'content' => sprintf( '{"longUrl": "%s"}', trim( $url ) )
       )
   );

   return json_decode(

             file_get_contents( 'https://www.googleapis.com/urlshortener/v1/url',

                 FALSE, stream_context_create( $opts )
             )
          );
}

$short = GoogleUrlShortener( 'http://code.imasters.com.br' );

var_dump( $short -> id ); // Algo como: http://goo.gl/JTEP

Detalhes importantes:

 

- Requerimento mínimo: PHP 5 ou superior

 

- OpenSSL habilitado, para assim haver suporte ao protocolo https

 

Originalmente postado no iMasters CODE

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.