PHP - Url Shortening
So a friend is making a URL shortening site, well hes already made it and got it working but he wanted me to do some work on it and then he showed me the code and I just felt like shooting myself in the head for offering to help. Its an absolute mess the are tonnes of folders with tonnes of files in each with more folders in them that contain code that could easily be functions. He said he added on top of some other code he got from somewhere.
So I'm thinking of just starting again. Everything that needs to be done I can do except for the URL shrinking. How do I make a redirect without actually creating the file. So say www.clickme.com/randomnumbers but that random numbers isn't a file and just comes up with a page with adverts then a link to continue on. Similar TutorialsHi, Right I have a button which links to a dynamic page and I want to clean up the URL, at the moment the button looks like this: echo "<a href='$page?\"?goto=true&id=".$custID."'>Edit</a>"; and I basically want to remove ** \"?goto=true&id=".$custID ** from the URL bar in the browser! How can this be done. Regards Lee 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 Code: [Select] 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); |