Compdigitec Labs

« | Home | »

Getting the direct link of a SkyDrive file transparently using PHP

By admin | January 31, 2011

Windows Live SkyDrive is one of the best online storage/file hosting solutions available online. It provides you with 25 GBs of free storage associated with your Windows Live ID, and allows you to upload as many files as you want to it. However, Windows Live SkyDrive’s direct links are not stable for more than a day, unlike Google Docs where the direct link is stable and permanent. Here is a PHP function that can be used to obtain the direct link of a SkyDrive file using the public URL of the file.

<?php
/*
 *      (C) Copyright 2011 Compdigitec. All rights reserved.
 *      Redistribution and use in source and binary forms, with or without
 *      modification, are permitted provided that the following conditions are
 *      met:
 *
 *      * Redistributions of source code must retain the above copyright
 *        notice, this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following disclaimer
 *        in the documentation and/or other materials provided with the
 *        distribution.
 *      * Neither the name of the Compdigitec nor the names of its
 *        contributors may be used to endorse or promote products derived from
 *        this software without specific prior written permission.
 *
 *      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *      "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *      LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *      A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *      OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *      SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *      LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *      DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *      THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *      (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *      OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * Gets the direct link of a URL with a given public Skydrive URL
 * @param string $liveurl The public SkyDrive URL
 * @return string|false The direct URL of the skydrive or false if error
 */
function get_skydrive_direct_link($liveurl) {
	$file = file_get_contents($liveurl);

	// find file loc
	$pos = strpos($file,'<a id="spPreviewLink" href="');
	if($pos === false) return false;
	$pos += strlen('<a id="spPreviewLink" href="');

	$buffer = "";
	while(substr($file,$pos,1) != '"') {
		$buffer .= substr($file,$pos,1);
		$pos++;
	}

	$buffer = html_entity_decode($buffer);

	$directlink = substr($buffer,0,strlen($buffer)-7);

	$host = parse_url($directlink,PHP_URL_HOST);
	if(strpos($host,"livefilestore.com") === false) {
		return false;
	}
	return $directlink;
}
?>

To use this function, feed it the public URL and it will return the direct link of the file:

$direct = get_skydrive_direct_link("http://cid-abcdef1234567890.office.live.com/self.aspx/FolderName/FileName.exe");
// Returns something like http://public.bay.livefilestore.com/y1pZwjyu-qfXUWjM-si2IeNs-juO0PjZE-Sk5EVvZxgeenjCxxv9vHiUy8q1RGafKnbaGSC2alqAoIbDvAYegznaZ/FileName.exe
$not_direct = get_skydrive_direct_link("http://not.skydrive.example.com/not/a/skydrive/FileName.exe");
// Returns FALSE (if it is not a SkyDrive upload)

You can use this script as part of an independent web service or as part of your existing PHP application under the BSD license. There is a link generator on this website available that you can use to try it out or use with your application without needing the script to be installed.

If you found this article helpful or interesting, please help Compdigitec spread the word. Don’t forget to subscribe to Compdigitec Labs for more useful and interesting articles!

Topics: PHP | 8 Comments »

8 Responses to “Getting the direct link of a SkyDrive file transparently using PHP”

  1. Tim Acheson Says:
    March 16th, 2011 at 6:57 am

    Nice! I tried this, it works nicely.

    I created an unofficial SkyDrive API called “SkyScraper”, available as a compiled DLL for .NET, which provides a method for getting the direct URL for a file on SkyDrive:

    http://www.timacheson.com/Blog/2009/oct/skydrive_api

  2. Waqar Ahmad Says:
    May 7th, 2011 at 5:39 am

    Hi, Chekout this video, This is showing to get realtime direct links on your page without doing any backend script. It uses YQL and JQuery
    http://www.youtube.com/watch?v=QcGehYzJHqs

  3. admin Says:
    May 13th, 2011 at 6:02 am

    @Waqar:

    Your solution relies on JavaScript and doesn’t provide a “real” direct link. It might work for your purposes but it wasn’t what this was designed for, since you can’t feed that kind of thing to something like wget.

  4. Hans Henrik Says:
    March 7th, 2012 at 10:39 am

    $buffer = “”;
    while(substr($file,$pos,1) != ‘”‘) {
    $buffer .= substr($file,$pos,1);
    $pos++;
    }

    could be
    $buffer=substr($file,$pos,strpos($file,’”‘,$pos));

  5. Tom Says:
    December 16th, 2012 at 5:58 pm

    Sorry, where I can find something like this but on JavaScript?

  6. parimala Says:
    March 12th, 2013 at 5:16 am

    how can i get get_skydrive_direct_link($liveurl) the live url of the file from skydrive ..

    can u explain me about the live url structure please?

  7. Hung Ti Says:
    May 9th, 2013 at 12:44 pm

    Sorry, I see your liveurl structure is http://cid-abcdef1234567890.office.live.com/self.aspx/FolderName/FileName.exe but i get an url like this: https://skydrive.live.com/redir?resid=251ACD38E38EACD7!132&authkey=!ABgdudnDpH-FMl8 when i get the link from skydrive. Can you help me with this situation please.

  8. admin Says:
    May 11th, 2013 at 1:16 am

    @Hung Ti:

    I guess Microsoft updated the Skydrive pages in the meantime.

Comments