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!


  • foredipinrang

    Not to mention those peppers both sweet and hot iin all of the thnose attractive colords and thoroughly indispensable for your
    metabolism. On the other hand, inside the traditional system of injecting tthese cosmetic fillers boasts a needle directly pushed in your skin.
    Men’s pleasant appearance and youth are dependent largely about the firmness and
    evennrss of their complexion.

  • Esperanza

    I actually wanted to post a comment in order to express gratitude to you for these pleasant ideas
    you are giving at this website. My long internet investigation has finally been recognized with awesome facts and strategies to exchange with my
    neighbours. I ‘d repeat that we readers are very endowed to exist in a wonderful community
    with very many awesome professionals with very helpful secrets.
    I feel very much grateful to have used your entire website page and
    look forward to tons of more cool minutes reading here.
    Thanks a lot again for a lot of things.

  • gasa oil

    They help in opening the blockedd airway and continue tto prevent any
    further attack of asthma. Most of my clients notice a difference of tgeir breathing
    from the first ten mins, typically a 20-30% reduction in overall breathing restriction.
    You ccan now easily grab tjis solution online lookin at the official website.

  • Pro Keto Rx

    I would like to thank you for the efforts you’ve put in writing this blog.

    I’m hoping to see the same high-grade blog posts by you in the future as well.
    In fact, your creative writing abilities has motivated me to
    get my own, personal website now ;)

  • Foredi Palu

    Astelin nnot contain pseudoephedrine, but features its own natural decongestant effects built-in. If uup your eyes start itching and dificulty
    it really is you attempting to reduce itching, rubbing, and you will not gget allergy medicine, you might consider rinsing uup your eyes wijth cold water.
    Ragweed term used to refer to a variety of plants with the genus Ambrosia.


    If you want to take much from this piece of writing then you have to apply such techniques
    to your won blog.


    No matter if some one searches for his vital thing, thus he/she desires
    to be available that in detail, therefore that thing
    is maintained over here.


    I am not certain the place you’re getting your information, but good topic.
    I needs to spend some time studying more or figuring out
    more. Thank you for magnificent info I was in search of this info for my mission.

Leave a Reply

Your email address will not be published. Required fields are marked *