Jump to content

Recommended Posts

I created a functional class that shortens or expands the URL using google's shortening service. The class works but i feel like i can optimize it more but i don't know really how because i'm new in using classes.

here is the code

define('GOOGLE_API_KEY', 'khjdaskjfsjkdhgfbsdhgsj');
define('GOOGLE_ENDPOINT', 'https://www.googleapis.com/urlshortener/v1');

class shortenGoogle {
function shortenUrl($longUrl){
	// initialize the cURL connection
	$ch = curl_init(sprintf('%s/url?key=%s', GOOGLE_ENDPOINT, GOOGLE_API_KEY));

	// tell cURL to return the data rather than outputting it
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	// create the data to be encoded into JSON
	$requestData = array(
		'longUrl' => $longUrl
	);

	// change the request type to POST
	curl_setopt($ch, CURLOPT_POST, true);

	// set the form content type for JSON data
	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));

	// set the post body to encoded JSON data
	curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestData));

	// perform the request
	$result = curl_exec($ch);
	curl_close($ch);

	// decode and return the JSON response
	return json_decode($result, true);
}

function expandUrl($shortUrl){
	// initialize the cURL connection
	$ch = curl_init(sprintf('%s/url?key=%s&shortUrl='.$shortUrl.'', GOOGLE_ENDPOINT, GOOGLE_API_KEY));

	// tell cURL to return the data rather than outputting it
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

	// tell cURL to ignore the SSL certificate in case it expires
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

	// perform the request
	$result = curl_exec($ch);
	curl_close($ch);

	// decode and return the JSON response
	return json_decode($result, true);
}
}
$short = new shortenGoogle;
$url = $short->shortenUrl('http://www.google.com/');
print_r($url);

Link to comment
https://forums.phpfreaks.com/topic/256176-google-shortening-service-class/
Share on other sites

Get rid of the constants. Pass in the Google API key to the constructor to store within a property, and move the end point URL constant to a class constant. You could probably centralise most of the curl functions into a separate method too, and just pass in the URL for each request. Personally though I would separate the two methods into their own classes, as they do different things.

 

thank you Adam, I made a few changes in moving the api key and the url into the class but i cannot make the __construct :( i looked over the manual but i cannot make it work still need 2 understand more about class logic.

<?php
class shortenGoogle {
public $google_api_key = 'sadasdfasfasfds';
const GOOGLE_ENDPOINT = 'https://www.googleapis.com/urlshortener/v1';

function shortenUrl($longUrl){
	// initialize the cURL connection
	$ch = curl_init(self::GOOGLE_ENDPOINT.'/url?key='.$this->google_api_key);

	// tell cURL to return the data rather than outputting it
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	// create the data to be encoded into JSON
	$requestData = array(
		'longUrl' => $longUrl
	);

	// change the request type to POST
	curl_setopt($ch, CURLOPT_POST, true);

	// set the form content type for JSON data
	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));

	// set the post body to encoded JSON data
	curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestData));

	// perform the request
	$result = curl_exec($ch);
	curl_close($ch);

	// decode and return the JSON response
	return json_decode($result, true);
}

function expandUrl($shortUrl){
	// initialize the cURL connection
	$ch = curl_init(self::GOOGLE_ENDPOINT.'/url?key='.$this->google_api_key.'&shortUrl='.$shortUrl.'');

	// tell cURL to return the data rather than outputting it
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

	// tell cURL to ignore the SSL certificate in case it expires
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

	// perform the request
	$result = curl_exec($ch);
	curl_close($ch);

	// decode and return the JSON response
	return json_decode($result, true);
}
}
$short = new shortenGoogle;
$url = $short->shortenUrl('http://www.google.com/');
$url2 = $short->expandUrl('http://goo.gl/fbsS');
print_r($url);
print_r($url2);
?>

You wouldn't hard-code the API key within the class, just declare the property (with no value). As you construct the object you would then pass it in as a parameter:

 

$short = new shortenGoogle('sadasdfasfasfds');

 

The constructor method now just needs to assign the passed value to the property:

 

public function __construct($google_api_key)
{
    $this->google_api_key = $google_api_key;
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.