dsp77 Posted February 1, 2012 Share Posted February 1, 2012 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); Quote Link to comment https://forums.phpfreaks.com/topic/256176-google-shortening-service-class/ Share on other sites More sharing options...
Adam Posted February 1, 2012 Share Posted February 1, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256176-google-shortening-service-class/#findComment-1313283 Share on other sites More sharing options...
dsp77 Posted February 1, 2012 Author Share Posted February 1, 2012 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/256176-google-shortening-service-class/#findComment-1313304 Share on other sites More sharing options...
Adam Posted February 1, 2012 Share Posted February 1, 2012 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; } Quote Link to comment https://forums.phpfreaks.com/topic/256176-google-shortening-service-class/#findComment-1313339 Share on other sites More sharing options...
dsp77 Posted February 2, 2012 Author Share Posted February 2, 2012 i swear i did that at one point, but thank you!!! now i got it Quote Link to comment https://forums.phpfreaks.com/topic/256176-google-shortening-service-class/#findComment-1313610 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.