Get the Real URL of a Tiny URL in PHP

October 21, 2013

A while back I set up a website,, that allows me to post Youtube videos of songs I really like. But, I’m lazy, and I wanted to use the least amount of effort to accomplish this.

I ended up setting up a If This Then That recipe for the hashtag “#songdump.” So, whenever I share a song through Twitter, IFTTT grabs the URL and through some other scripts, retrieves all the necessary info I need for that video, and posts it in WordPress (where the data is called from).

Problem is, when you share something on Twitter, it’s often a TinyURL which makes it a lot harder to retrieve info from. Luckily, we can use the PHP function “get_headers()” to accomplish this.

Tiny URL’s come in a lot of different forms, so I used this method because alternatives often had you filter based on a predetermined set of base URLs. That did me no good.

Here’s the script:

Let’s recap what’s happening line by line here –

  1. Storing our tiny URL as a variable, showing 2 examples
  2. Using the get_headers() function to retrieve the location of our URL
  3. This returns an array, and one of values in that array is our URL. It begins with “Location:” or is some cases “location:”. Feel free to use print_r($headers) function before this to learn all of get_headers() returned values
  4. The key for this value is often in different orders (which is why I provided 2 URLs as examples). We will loop through every value to see if it contains “ocation:” (because as I said, sometimes it’s uppercase, sometimes it’s lowercase) and then return that key.
  5. Once we find the right array object, we get its value and store it as a variable.
  6. Using a string replace function, we remove the actual “Location” label. I do this twice, uppercase and lowercase, because it can get returned either way in some cases.
  7. Then we print out the real URL!

Hope that helps! Happy coding!

PostFix Local Loop in PLESK

April 12, 2012


The problem

A while back, I was having an issue with my mail not being delivered. I would write a function using

and it would successfully send an email to every address EXCEPT one particular address. I couldn’t tell why. This is something that occurs on Parellels’ PLESK server administration software.

The solution

I’ll get to it awfully quick, then I’ll tell you how I figured it out.

First, you need to ask yourself, is this email address the same domain being hosted on PLESK? Bingo. The problem was that when PostFix (the SMTP sendmail uses by default in most cases) determined the address to be the same as the domain it was hosting, it did a local search to find that address rather than just sending the email.

Naturally, if you didn’t have that particular email address set up, or any email with that extension set up on the server, it found nothing, and wouldn’t send the email. There is a very easy fix to this issue by commenting out 3 lines in your Postfix ‘’ configuration file. This was originally documented by Luke Tarplin, and I can’t explain how helpful it was.

Find your ‘’ configuration file for PostFix, which for CentOS 6, is located at

If you can’t find it, do a

SSH command to at least see where Postfix is on your server.

Then, open the file up through a text editor, or in the Linux shell, and make these lines (should be at the end of the file, around line 677) :

commented out like this :

Then, restart the Postfix service, and Apache while your at it (can’t hurt), and voila! Your email address should be receiving those emails now. This also doesn’t affect any of your regular emails or anything else, either.

Now, I figured out how to look for this by checking over my mail logs. Any time you have an error with sending an email, check your mail logs. For CentOS, they are located:

although for most Linux OS, they are located

On mine, I noticed that the output for that particular email kept saying

On line 2, you can see that Postfix begins a

search. That was a dead giveaway that for some reason PostFix was reverting to a local search. I put 2 and 2 together and starting searching for the appropriate solution.

If, for some reason, you happen to have similar circumstances but this isn’t the right solution, just remember : Always check your mail logs. It has everything you could ever need to know in fixing a problem. Good luck and happy coding!