New API: Expand URL

Today, the Internet is flooded with shortened URL. All links shared on Twitter are shortened (start with t.co). There is nothing wrong with it. I just don’t feel comfortable clicking a shortened URL without knowing where it will bring me to. How paranoid I am! There are several reason I don’t trust shortened URL: They might contain tracking or referral code. SlickDeals heavily uses it (to make money, of course). Again, there is nothing wrong with it. They provide you a service and they deserve to get some money out of it. They might hide malicious URL, like XSS I hate them! There are several expand URL services out there, however they just support a very limited set of URL shorten service. What I want is something more. I want to be able to track the final URL after a bunch of redirections. So I wrote this API: http://api.codepie.org/expand_url/<your_url_here_no_escape_needed> For example: http://api.codepie.org/expand_url/http://slickdeals.net/?pno=128958&lno=1&afsrc=1&trd=Newegg+com Here is the API result. You will be amazed how many hops it take to the final destination: http://slickdeals.net/?pno=128958&lno=1&afsrc=1&trd=Newegg+com http://www.jdoqocy.com/click-1225267-10440897?sid=1fea0918ac5e454fa2fc89be4e70d43b&url=http%3A%2F%2Fwww.newegg.com%2FProduct%2FProduct.aspx%3Fsdtid%3D7164970%26SID%3D1fea0918ac5e454fa2fc89be4e70d43b%26AID%3D10440897%26PID%3D1225267%26nm_mc%3DAFC-C8Junction%26cm_mmc%3DAFC-C8Junction-_-cables-_-na-_-na%26Item%3D03V-003G-00433 http://cj.dotomi.com/a6106ar-zH/ry2/GFJJFNOM/GHHKHLM/F/F/F?c=v5vq%3DEsrnDMELnpIrHIHsnFspLMorHrKDqHGo%2674y%3Du662%25GN%25FS%25FS999.0r9rtt.p1z%25FSc41q7p6%25FSc41q7p6.n52A%25GS5q6vq%25GQKEJHMKD%25FJfVQ%25GQEsrnDMELnpIrHIHsnFspLMorHrKDqHGo%25FJNVQ%25GQEDHHDLMK%25FJcVQ%25GQEFFIFJK%25FJ0z_zp%25GQNSP-PLW70p6v10%25FJpz_zzp%25GQNSP-PLW70p6v10-_-pnoyr5-_-0n-_-0n%25FJV6rz%25GQDGi-DDGT-DDHGG%3C%3Cu662%3A%2F%2F999.wq131pB.p1z%3ALD%2Fpyvpx-EFFIFJK-EDHHDLMK%3C%3CT%3Cu6625%3A%2F%2F999.t11tyr.p1z%2F%3C http://www.emjcd.com/h4102tenm6/elq/32662AB9/3447489/2/39292468268596B574/87Ad4g9d4ggd33g6A24f3A25952f57g5?c=zC2x%3DLzyuKTLSuwPyOPOzuMzwSTvyOyRKxONv%26EB5%3D1DD9%25NU%25MZ%25MZGGG.7yGy00.w86%25MZjB8xEwD%25MZjB8xEwD.uC9H%25NZCxD2x%25NXRLQOTRK%25MQmcX%25NXLzyuKTLSuwPyOPOzuMzwSTvyOyRKxONv%25MQUcX%25NXLKOOKSTR%25MQjcX%25NXLMMPMQR%25MQ76_6w%25NXUZW-WSdE7wD287%25MQw6_66w%25NXUZW-WSdE7wD287-_-wuv5yC-_-7u-_-7u%25MQcDy6%25NXKNp-KKNa-KKONN%3Cw38!GQ96-PAKTEzy%3C1DD9%3A%2F%2FGGG.3x8A8wI.w86%3ASK%2Fw52w4-LMMPMQR-LKOOKSTR%3C%3Ca%3C1DD9C%3A%2F%2FGGG.08805y.w86%2F%3C http://www.newegg.com/Common/AdvancedLinkLanding.aspx?nm_mc=AFC-C8Junction&cm_mmc=AFC-C8Junction-_-na-_-na-_-na&AID=10440897&PID=1225267&SID=1fea0918ac5e454fa2fc89be4e70d43b&URL=http%3A%2F%2Fwww.newegg.com%2FProduct%2FProduct.aspx%3Fsdtid%3D7164970%26SID%3D1fea0918ac5e454fa2fc89be4e70d43b%26AID%3D10440897%26PID%3D1225267%26nm_mc%3DAFC-C8Junction%26cm_mmc%3DAFC-C8Junction-_-cables-_-na-_-na%26Item%3D03V-003G-00433 http://www.newegg.com/Product/Product.aspx?sdtid=7164970&SID=1fea0918ac5e454fa2fc89be4e70d43b&AID=10440897&PID=1225267&nm_mc=AFC-C8Junction&cm_mmc=AFC-C8Junction-_-cables-_-na-_-na&Item=03V-003G-00433&cm_sp= Suggestions welcome!

How to format bytes to kilobytes, megabytes, gigabytes in PHP

This is the best function I found on StackOverflow: function formatBytes($bytes, $precision = 2) { $units = array(‘B’, ‘KB’, ‘MB’, ‘GB’, ‘TB’); $bytes = max($bytes, 0); $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); $pow = min($pow, count($units) – 1); // Uncomment one of the following alternatives // If 1 kilobytes = 1024 bytes // $bytes /= pow(1024, $pow); // If 1 kilobytes = 1000 bytes // $bytes /= (1 < < (10 * $pow)); return round($bytes, $precision) . ' ' . $units[$pow]; }

PHP: eval() without output to screen

Sometimes, we need to eval() some lines of code and grab the result without output to screen. As you know, eval() has only one parameter, so we have to use output buffer for that purpose. The following code can achieve that. Pretty clean and understandable: ob_start(); eval(‘echo “Hello world!”;’); $str = ob_get_contents(); ob_end_clean(); $str will hold the output returned by eval function.

List contents of a directory by date in PHP

Sometimes, you need to list all contents of a directory, for example, to display all images in a specified directory. Unfortunately, PHP native functions to handle files and directories have a very limited capabilities. For example readdir, opendir and much more only takes 1 parameter. You might wonder how to list contents of a directory by date. Here is one solution for that problem: function listdir_by_date($path){ $dir = opendir($path); $list = array(); while($file = readdir($dir)){ if ($file != ‘.’ and $file != ‘..’){ //To make sure you will not overwrite an array key, add filename at the end of datetime $ctime = filectime($data_path . $file) . ‘,’ . $file; $list[$ctime] = $file; } } closedir($dir); krsort($list); return $list; } Pretty simple, huh?

Remove one or more values from array in PHP

The solution is quite simple: Using built-in function array_diff of PHP: $dest_array = array_diff($src_array, array(“value_to_remove_1”, “value_remove_2”)); Of course you can use this method to remove just one value from array: $dest_array = array_diff($src_array, array(“value_to_remove”)); One problem of this method is that, there are some holes in $dest_array after apply this method. There are some ways of solving that problem: Remove all key strings from the array and replace them with numbers, use array_values Preserve the key names (strings), or reindex the array if all keys are numerical, use array_merge