Jump to content

Goo.gl Integration


daniel.s
 Share

Recommended Posts

Hi,

I'm trying to integrate google shorterner for use in social share buttons. I'm working with a goo.gl class to talk with the API but after this im kinda stuck.

I want every page that uses given template to get a short url and then echo it to the social share buttons. I putted this code in the top of my template for testing but can't even get the test to work yet. cURL is enabled on the server. Is it possible to do something like this or should i hook the function to some Processwire API call that triggers when the page is created?

Here's the code:

<?php 

require_once('Googl.class.php');

$googl = new Googl('YOUR_API_KEY');

// Shorten URL
$shortUrl = $googl->shorten('http://www.google.com/');
echo $shortUrl;

?>

Anyone who can point me in the right direction? Thanks

/Daniel

EDIT: After some further digging and tests i found my API key to be invalid.

  • Like 1
Link to comment
Share on other sites

  • 4 weeks later...

I noticed the script makes a new shortUrl for every visitor. I understand this is due the script is run every page load. How can i get a static short url for every page using the template instead of loading a new short url every page load?

Link to comment
Share on other sites

Just a quick thought...

You could create a field (e.g. a text field  - just use a URL field called 'shorturl') and include that in your template for the pages that you want to have a short URL. Then, use an if statement to check whether the shorturl field is empty. If it is empty  it means no short URL has been created, so include the Google URL shortener code. Once it creates the short URL, save the shortened URl in that text URL field  (maybe it could be a URL field as well but PW would probably attempt to modify it :-)) and echo it out to the social share buttons. If the field is not empty, it means there is already a short URL, so just echo that one to the social buttons. 

Hope this makes sense :-)

Edit: Use a URL field; works fine - see below

Edited by kongondo
  • Like 1
Link to comment
Share on other sites

<?php

require_once('Googl.class.php');

$googl = new Googl('YOUR_API_KEY');

if (!$page->shortUrl) {
  page->$shortUrl = $googl->shorten('http://www.google.com/');
} 

?>

How can i do the if statement if the field isn't populated already? is !$page->shortUrl or how can it be done. And do i need to get something like $field->set($key, $value) ? 

Im not that great with PHP, thanks for all the help.

/Daniel

Link to comment
Share on other sites

Edit: Here's some code. Note, I am using a URL field...works fine...

if ($page->links) {
	echo "<p>The existing short url is :" . $page->links . "</p>";
}

else {
       require_once('Googl.class.php');
       $url = new GoogleURL('YOUR_API_KEY');
	// Shorten URL
	$shortUrl = $url->shorten($config->httpHost . $page->url);
	
	$page->of(false);
	$page->links = $shortUrl;//this is a URL field
	$page->save('links');//we save only this field (see Adrian's post below)
		
	echo "<p>The shortened url is :" . $page->links . "<p>";

}
 
Edited by kongondo
  • Like 2
Link to comment
Share on other sites

Having said that....in my tests, I have noticed that Google is intelligent enough to know if I have already created a short URL for a page if I am using the same API key...Attempts to recreate just give me back the same short URL it created earlier...Anyway... :-)

Edited by kongondo
Link to comment
Share on other sites

Oops, in my example, I had commented out //require_once('Googl.class.php'); This is because I had the Class already in my template file...Don't forget to uncomment yours! :-)...if you do, PHP will tell you anyway...

Link to comment
Share on other sites

  • 1 year later...

The script has been working really great for a year now but for some strange reason it's decided to stop working. Can't find any deprecated code used in the code and the shortened url's comes up in my goo.gl account but won't work in the $page->links field. Anyone have a clue what's wrong? Been scratching my head trying to figure this out for days now :(

if($page->links) {
  $short = $page->links;
} else {
  $shortUrl = $client->shorten($config->httpHost . $page->url);
  $page->of(false);
  $page->links = $shortUrl["id"]; // About here something's going wrong
  $page->save('links');
  $short = $page->links;
}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...