Alfro
Alfro

Reputation: 1544

trying to download with curl but can't decode the download url string

I'm trying to download with curl from this url:

http://utorrent.en.softonic.com/download

When I select the alternative download link with my php script, I get this url:

http://utorrent.en.softonic.com/download-tracker?th=1%2F6CH9aeXedl4L8u%2BBHNJXWTW%2BLP1LFlnGQpxqjlxAMoVKXinh0rsXkMa8DN%2Bu6VBtArq5kfHl7Y2RJqLz%2FQUFTYOfbBsY4c9edz4d0uBydqkfciVN7cTFTjgf%2BVtTnzLV%2F4y3COCfojCKefPsPrRevbDp7d8SrrFDue95ZjhblKzCHHqpNk5gpQMihnZewg%2Fyc95fQne0DfkIfh%2BifbqX%2FCBkTDiekrGjfGamf9P%2BEll7oSfXnb9SLZg9H76ancy%2BinY3u8GE7HAJTsmqcaD4WPGon5rZYcnS8aljq7CPWLxysaKFOBu0VTr99hDNv%2Fd6rsepVexoymYrP2Hg6pe3strNwvWWePZXBsmekvAGt76yLP8R3mo5nqoBKu497L%2Fz4yQ%2BQCySXk9eqDKSE0MjByPorUPuqXZ9EMmi1KtSqX6HFvg6moZrG9Eg46eNZAY%2F9jhm6NhCV51pF6kWsnBtg4UfBJLKpQFLElRSt5vYfOQ7GE44TTPD7DpmF4XAZioF0ceMqr%2BEzZ6b3N1LY1Pwjsg7EYiTIyUmRtzKsI8WS7HB51H3bJqzF2HhUdVYVJt5U33G9VF0%2FwykMd7eJ%2FY5p6Ia6MmkpoWbe29UB3TzbVPUc%2F0t4wc8R2qkZK7Cqx5vvm3rCPViTpOQiq4ItJNTkM7DYamDyIpice8VB%2Fm7SBM9SOboRDewuQTNEDrIw3yGjfakXSGvFTeAS6j35ikg%3D%3D

but curl is downloading empty files. At the beginning I thought it was a bad config but I've tryed the url in the browser and it doesn't work, so finally I've figured they are using some kind of codification for the url but I have no idea which one because I'm very stuck in this domain. I've been checking the traffic with httpfox and there is a 302 redirect, curl is configured to follow redirects and after reading a lot of posts about curl config here, I think everything is ok on this side.

$ch = curl_init();
$output_filename = "test.exe";
curl_setopt($ch, CURLOPT_URL, $downloadUrl);
            curl_setopt($ch, CURLOPT_VERBOSE, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_AUTOREFERER, false);
            curl_setopt($ch, CURLOPT_REFERER, $previousUrl);
            curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            $result = curl_exec($ch);
            $dl=curl_error($ch);
            curl_close($ch);

$fp = fopen($output_filename, 'w');
            fwrite($fp, $result);
            fclose($fp);
            die('download complete');

So my question is does anybody know what kind of codification is using this url string and how can I decode it?

Upvotes: 0

Views: 205

Answers (1)

drew010
drew010

Reputation: 69937

Tested and working:

<?php

$url = 'http://utorrent.en.softonic.com/download';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIEFILE, '');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2450.0 Iron/46.0.2450.0 Safari/537.36');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($ch);

libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHtml($result);

$xpath = new DOMXPath($doc);
$link  = $xpath->query('//*[@id="download-button"]');

if ($link->length == 0) {
    die('Failed to find download link on page');
}

$url = $link->item(0)->attributes->getNamedItem('href')->nodeValue;

curl_setopt($ch, CURLOPT_URL, $url);

$result = curl_exec($ch);

// TODO: check HTTP response code and result for errors

file_put_contents('/tmp/utorrent.exe', $result);

Upvotes: 1

Related Questions