{"id":208,"date":"2011-01-31T21:02:37","date_gmt":"2011-02-01T02:02:37","guid":{"rendered":"http:\/\/www.compdigitec.com\/labs\/?p=208"},"modified":"2011-02-02T06:52:02","modified_gmt":"2011-02-02T11:52:02","slug":"getting-the-direct-link-of-a-skydrive-file-transparently-using-php","status":"publish","type":"post","link":"http:\/\/www.compdigitec.com\/labs\/2011\/01\/31\/getting-the-direct-link-of-a-skydrive-file-transparently-using-php\/","title":{"rendered":"Getting the direct link of a SkyDrive file transparently using PHP"},"content":{"rendered":"<p><a href=\"http:\/\/skydrive.live.com\/\">Windows Live SkyDrive<\/a> 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&#8217;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.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\/*\r\n *      (C) Copyright 2011 Compdigitec. All rights reserved.\r\n *      Redistribution and use in source and binary forms, with or without\r\n *      modification, are permitted provided that the following conditions are\r\n *      met:\r\n *\r\n *      * Redistributions of source code must retain the above copyright\r\n *        notice, this list of conditions and the following disclaimer.\r\n *      * Redistributions in binary form must reproduce the above\r\n *        copyright notice, this list of conditions and the following disclaimer\r\n *        in the documentation and\/or other materials provided with the\r\n *        distribution.\r\n *      * Neither the name of the Compdigitec nor the names of its\r\n *        contributors may be used to endorse or promote products derived from\r\n *        this software without specific prior written permission.\r\n *\r\n *      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r\n *      &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\r\n *      LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\r\n *      A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\r\n *      OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\r\n *      SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r\n *      LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r\n *      DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r\n *      THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r\n *      (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r\n *      OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r\n *\/\r\n\r\n\/**\r\n * Gets the direct link of a URL with a given public Skydrive URL\r\n * @param string $liveurl The public SkyDrive URL\r\n * @return string|false The direct URL of the skydrive or false if error\r\n *\/\r\nfunction get_skydrive_direct_link($liveurl) {\r\n\t$file = file_get_contents($liveurl);\r\n\r\n\t\/\/ find file loc\r\n\t$pos = strpos($file,'&lt;a id=&quot;spPreviewLink&quot; href=&quot;');\r\n\tif($pos === false) return false;\r\n\t$pos += strlen('&lt;a id=&quot;spPreviewLink&quot; href=&quot;');\r\n\r\n\t$buffer = &quot;&quot;;\r\n\twhile(substr($file,$pos,1) != '&quot;') {\r\n\t\t$buffer .= substr($file,$pos,1);\r\n\t\t$pos++;\r\n\t}\r\n\r\n\t$buffer = html_entity_decode($buffer);\r\n\r\n\t$directlink = substr($buffer,0,strlen($buffer)-7);\r\n\r\n\t$host = parse_url($directlink,PHP_URL_HOST);\r\n\tif(strpos($host,&quot;livefilestore.com&quot;) === false) {\r\n\t\treturn false;\r\n\t}\r\n\treturn $directlink;\r\n}\r\n?&gt;\r\n<\/pre>\n<p>To use this function, feed it the public URL and it will return the direct link of the file:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$direct = get_skydrive_direct_link(&quot;http:\/\/cid-abcdef1234567890.office.live.com\/self.aspx\/FolderName\/FileName.exe&quot;);\r\n\/\/ Returns something like http:\/\/public.bay.livefilestore.com\/y1pZwjyu-qfXUWjM-si2IeNs-juO0PjZE-Sk5EVvZxgeenjCxxv9vHiUy8q1RGafKnbaGSC2alqAoIbDvAYegznaZ\/FileName.exe\r\n$not_direct = get_skydrive_direct_link(&quot;http:\/\/not.skydrive.example.com\/not\/a\/skydrive\/FileName.exe&quot;);\r\n\/\/ Returns FALSE (if it is not a SkyDrive upload)\r\n<\/pre>\n<p>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 <a href=\"http:\/\/test.compdigitec.com\/skydrive\/\">link generator on this website<\/a> available that you can use to try it out or use with your application without needing the script to be installed.<\/p>","protected":false},"excerpt":{"rendered":"<p>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&#8217;s direct links are not stable for more than a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[743,864,742,595],"_links":{"self":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts\/208"}],"collection":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/comments?post=208"}],"version-history":[{"count":0,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/posts\/208\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/media?parent=208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/categories?post=208"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.compdigitec.com\/labs\/wp-json\/wp\/v2\/tags?post=208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}