尝试使用curl下载但无法解码下载url字符串

时间:2015-11-25 17:46:32

标签: php curl

我正在尝试从此网址下载curl:

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

当我用我的php脚本选择替代下载链接时,我得到这个网址:

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

但curl正在下载空文件。一开始我认为这是一个糟糕的配置,但我在浏览器中尝试了网址并且它不起作用,所以最后我认为他们正在使用某种编码为网址,但我不知道哪一个因为我非常困在这个领域。我一直在用httpfox检查流量,并且有一个302重定向,curl配置为遵循重定向,在阅读了很多关于curl配置的帖子之后,我认为这一切都很好。

$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');

所以我的问题是有人知道使用这个url字符串是什么样的编码,我该如何解码呢?

1 个答案:

答案 0 :(得分:1)

经过测试和工作:

<?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);