## 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
/*
*      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.

Topics: PHP | 21 Comments »

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

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

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

May 7th, 2011 at 05:39

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

May 13th, 2011 at 06:02

@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

$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 17:58

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

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

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

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

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.

May 11th, 2013 at 01:16

@Hung Ti:

I guess Microsoft updated the Skydrive pages in the meantime.

9. slot999 Says:
March 16th, 2022 at 07:53

[…] Information on that Topic: compdigitec.com/labs/2011/01/31/getting-the-direct-link-of-a-skydrive-file-transparently-using-php/ […]

10. marlin firearms for sale Says:
April 1st, 2022 at 06:19

[…] Find More to that Topic: compdigitec.com/labs/2011/01/31/getting-the-direct-link-of-a-skydrive-file-transparently-using-php/ […]

11. kspods.com Says:
May 9th, 2022 at 20:31

[…] Information on that Topic: compdigitec.com/labs/2011/01/31/getting-the-direct-link-of-a-skydrive-file-transparently-using-php/ […]

12. สล็อตวอเลท Says:
May 29th, 2022 at 21:26

[…] Info on that Topic: compdigitec.com/labs/2011/01/31/getting-the-direct-link-of-a-skydrive-file-transparently-using-php/ […]

13. nova88 Says:
June 4th, 2022 at 03:52

14. eurocasino Says:
June 6th, 2022 at 06:48

Getting the direct link of a SkyDrive file transparently using PHP at Compdigitec Labs

https://congdongfifa.com/cristiano-ronaldo-va-con-trai/

15. meritroyalbet Says:
June 6th, 2022 at 07:04

Getting the direct link of a SkyDrive file transparently using PHP at Compdigitec Labs

https://www.globalhousingcompany.com/viviendas-u-oficinas-estructura-container/

16. meritroyalbet Says:
June 6th, 2022 at 07:17

Getting the direct link of a SkyDrive file transparently using PHP at Compdigitec Labs

https://www.klockman.se/2017/02/09/hej-varlden/

June 6th, 2022 at 07:22

Getting the direct link of a SkyDrive file transparently using PHP at Compdigitec Labs

https://www.purple-choice.com/increase-social-media-engagement-a-guide-for-marketers/

18. bahis siteleri Says:
June 10th, 2022 at 08:20

perfect

thank you for a very good article

19. condominios la ventana Says:
June 10th, 2022 at 20:19

[…] Information on that Topic: compdigitec.com/labs/2011/01/31/getting-the-direct-link-of-a-skydrive-file-transparently-using-php/ […]

20. meritroyalbet Says:
June 19th, 2022 at 13:40

Getting the direct link of a SkyDrive file transparently using PHP at Compdigitec Labs