Reputation: 43
here's the problem i have.Before OneDrive changed the url's it was easy to force a file to be downloaded from cURL, but now since they've changed their URL's construction i can't use the previous approach of replacing redir? with download?. That's how they used to be constructed:
https://onedrive.live.com/redir?resid=
To make it start downloading immediately we just had to replace the redir with download like this
https://onedrive.live.com/download?resid=
and that was fixing it at all, but now the url's are like this:
https://1drv.ms/u/s!AsdMSpdm_dmsadMSda
So, my question is how can i download the file from cURL, i cannot do that even with CURLOPT_FOLLOWLOCATION, it is only working if the file is forced to be downloaded. Thanks in advance!
Upvotes: 4
Views: 22577
Reputation: 6003
I found 3 ways to achieve that..
This option doesn't always work, because sometimes the redirected url also contains !
.
curl https://wie6yw.dm.files.1drv.com/y4mRNH415nArXijS4qtGN-bTq9plsHtfUJNq2M -L -o OUTPUTFILE.EXT
Just remember to rename OUTPUTFILE.EXT
in the end with the original filename.
This guy created an online tool to convert OneDrive links. It works, but if you do that in a daily basis this could be annoying.
I used the @Ryan Gregg's approach above and created a converter.php to convert OneDrive URLs and redirect the user to the final file. This way I can use it with cURL.
<?php
$sharedUrl = urldecode($_GET['url']);
$base64 = base64_encode($sharedUrl);
$encodedUrl = "u!" . rtrim($base64, '=');
$encodedUrl = str_replace('/', '_', $encodedUrl);
$encodedUrl = str_replace('+', '-', $encodedUrl);
$final = sprintf('https://api.onedrive.com/v1.0/shares/%s/root/content', $encodedUrl);
header('Location:' . $final, true, 302);
URL Encode the shared OneDrive URL. You can use an online tool if you want.
Call your php file in curl and pass the encoded URL as parameter in ?url=
. For example: curl http://localhost/converter.php?url=https%3A%2F%2F1drv.ms%2Fu%2Fs%21ApzSTOgc2rAs4d2olMS%3Fe%3DrZIUN -L -o OUTPUTFILE.EXT
.
Replace the OUTPUTFILE.EXT with the original file name.
Upvotes: 0
Reputation: 2035
There are two options. The "right" way to do this would be to encode the full sharing URL following the details described here https://learn.microsoft.com/en-us/onedrive/developer/rest-api/api/shares_get?view=odsp-graph-online#encoding-sharing-urls and then construct a URL like this:
https://api.onedrive.com/v1.0/shares/{encodedUrl}/root/content
Which will download the contents of the shared file at the URL. Option #2 is to splice the URL up and grab the path segment that starts with s!
and just use that in the same place as the encoded Url:
https://api.onedrive.com/v1.0/shares/s!.../root/content
Either way, you can then use cURL to download the content of the file.
Upvotes: 11