So, I've found this code somewhere on the net (don't remember) and I'm trying to embed it into my site, just in case some of the visitors would need to use it. It creates short URLs (from 4 services) of a particular address.
What I don't understand, and I hope someone could help me, is: why the code works when I test it locally, on my Windows+Xampp computer, but not on the server. The server is a centos 5, apache 2.23, php 5.1. I have suhosin installed, could this restrict the script, somehow!? I'm really lost, don't know what to try. I've chmoded the file in all the possible ways, I've enabled the allow_url_fopen, but still nothing. It doesn't extract/display the results.
Any help and suggestion would be much apreciated.
Thank you!
BTW, the address I'm testing it is http://atat.ro/utile/short-url/

The code:

<?php
function bitly($url) {
	$username = 'username'; // your bitly account
	$api = 'api key'; // your bitly password
	
	return getContent("http://api.bit.ly/v3/shorten?format=txt&longUrl=$url&login=$username&apiKey=$api");
}

function tinyurl($url) {
	return getContent("http://tinyurl.com/api-create.php?url=$url");
}

function google($url) {
	$data = getContent("http://ggl-shortener.appspot.com/?url=$url");
	$json = json_decode($data);

    return $json->short_url;
}

function isgd($url) {
	return getContent("http://is.gd/api.php?longurl=$url");
}

/**
 * Helper function for getting remote content
 *
 * Using cURL or file_get_contents
 */
function getContent($url) {
	$content = '';
	
	// use cURL
	if (function_exists('curl_init')) {
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		$content = curl_exec($ch);
		curl_close($ch);
	}
	// use file_get_contents
	elseif (ini_get('allow_url_fopen')) {
		$content = file_get_contents($url);
	}
	
	return $content;
}

if (!empty($_GET['url'])) {
	$url = rawurlencode($_GET['url']);
	
	$result = array();
	$result['bitly'] = bitly($url);
	$result['tinyurl'] = tinyurl($url);
	$result['google'] = google($url);
	$result['isgd'] = isgd($url);
	
	echo json_encode($result);
	
	exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Multiple URL Shortener</title>

<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->

<!--[if lt IE 9]>
<script src="http://ie7-js.googlecode.com/svn/version/2.1(beta4)/IE9.js"></script>
<![endif]-->

<style>
/* yui 2 reset */
body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,textarea,p,blockquote,th,td{margin:0;padding:0}table{border-collapse:collapse;border-spacing:0}fieldset,img{border:0}address,caption,cite,code,dfn,em,strong,th,var{font-style:normal;font-weight:normal}ol,ul{list-style:none}caption,th{text-align:left}h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:normal}q:before,q:after{content:''}abbr,acronym{border:0}

body{font:13px/1.5em Verdana,Arial,Helvetica,sans-serif;color:#333}
#container{width:500px;margin:30px auto}
header,section,footer{display:block}

header{margin-bottom:30px}
header h1{font:italic 30px Georgia,"Times New Roman",Times,serif;color:#888;text-align:center}

section{margin-bottom:30px}

#form{float:left;margin-bottom:20px}
input{float:left;border:1px solid #6baff0}
#url{width:350px;height:18px;padding:5px 15px;color:#999;margin-right:10px;-webkit-border-radius:30px;-moz-border-radius:30px;border-radius:30px}
#url:hover,#url:focus{background:#fefeec;color:#333}

#submit{height:30px;border-top:1px solid #96d1f8;background:#65a9d7;background:-webkit-gradient(linear,left top,left bottom,from(#9ec8f5),to(#65a9d7));background:-moz-linear-gradient(top,#9ec8f5,#65a9d7);padding:5px 10px;-webkit-border-radius:30px;-moz-border-radius:30px;border-radius:30px;color:#fff;font-size:13px;font-family:Helvetica,Arial,Sans-Serif}
#submit:hover{border-top-color:#3f98d4;background:#3f98d4;color:#fff}
#submit:active{border-top-color:#3f98d4;background:#3f98d4}

#shorturls{clear:both}
#shorturls p{margin-bottom:10px;line-height:1.5em}
#shorturls strong{font-size:18px;font-weight:normal;width:150px;display:inline-block;color:#4096ee}

footer{margin:15px auto}
</style>
</head>

<body>
<div id="container">
	<header>
		<h1>Multiple URL Shortener</h1>
	</header>

	<section>
		<form action="" method="get" id="form">
			<input type="text" name="url" id="url" value="Enter URL here..." onfocus="if(this.value==this.defaultValue)this.value=''" onblur="if(this.value=='')this.value=this.defaultValue" />
			<input type="submit" id="submit" value="Shorten it!" />
		</form>
		<div id="shorturls">
			<p><strong>Bit.ly</strong> <span id="bitly" class="result"></span></p>
			<p><strong>TinyURL</strong> <span id="tinyurl" class="result"></span></p>
			<p><strong>Google</strong> <span id="google" class="result"></span></p>
			<p><strong>Is.gd</strong> <span id="isgd" class="result"></span></p>
		</div>
	</section>

	<footer>
		Created by Rilwis. <a href="http://www.deluxeblogtips.com/2010/06/multiple-url-shortener-page.html">Return to article</a>.
	</footer>
</div> <!-- #container -->

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>

$('#form').submit(function(){
	// show loading icon
	$('.result').html('<img src="loading.gif" />');

	var url = $('#url').val();
	
	$.getJSON('', {url: url}, function(data){
		$('#bitly').text(data.bitly);
		$('#tinyurl').text(data.tinyurl);
		$('#google').text(data.google);
		$('#isgd').text(data.isgd);
	});
	
	return false;
});

</script>
</body>
</html>

Recommended Answers

All 7 Replies

The PHP script is not responding when the JSON request is made.

ie: $.getJSON()

This happens in the client side JS, but your PHP script on the server, will just hang. You'll have to figure out what is hanging. I'm guessing it is the HTTP requests made in the PHP script to the URL shortening services.

Try adding some debugging to your PHP script, like:

function getContent($url) {

echo "calling getContent with URL: $url\n";

etc.

I'm affraid I don't understand so good - could you please copy/paste exactly the code you think I should replace?! Also, I've noticed that IE8 will load another page with the $url in the address - trying to get short url for google.com will open the page, after submit, http://atat.ro/utile/short-url/?url=http%3A%2F%2Fgoogle.com

Thank you!

Anyone else who can help, please?

Well, I feel stupid. but I should write this, just in case someone else is having a similar issue: make sure you have the json extension installed if you plan to use json applications :)

However, is not entirelly solved. It seems that IE8, which unfortunatelly is used by most of my site' visitors, is still opening a new page after I click the submit button. please see at : http://atat.ro/utile/short-url/ (changed the location again). Firefox works fine, though.

Sorry, I take it back. It works with IE8, too, only if I enable the ad blocker is not working - therefore, the script is functioning, finally, as it should :)

Hello everyone!
Here is a one of shortest URL shortener websites with below spec:

I introduce you www.be.cm

  • Create short URLs using your memorable custom alias
  • Enter your long URL, then click on "Advanced Options" and enter your Custom Alias.
  • Access the statistic page by adding a "+" to the end of your short link e.g. http://www.be.cm/url+
  • Generate QR Code for your short URL
  • Share your short URL to Facebook and Twitter
  • Shorten multiple long URLs in one go
  • Complete track of your visitors e.g. works well if integrated in email marketing or links to track specific links
  • Password protected URLs
  • Add description to your short URL
  • Redirect users using the same URL if your have different pages for different countries
  • Referrers
  • Add be.cm to your browser's toolbar

Please feel free and feedback me

commented: Linkif.me is the best url shortner.. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.