我正在尝试获取令牌链接,但我不能。来源:
<span class="ipsHide" data-role="downloadCounterContainer">Download begins in <span data-role="downloadCounter"></span> seconds</span>
<a href='https://example.com/forum/files/file/2-content/?do=download&r=24050&confirm=1&t=1&csrfKey=72c4d0ffcabb34c6d6c490cbacbc354ag1536355261' class='ipsButton ipsButton_primary ipsButton_small' data-action="download" >Download</a>
爆炸:
$url = "link";
$content = file_get_contents($url);
$first = explode( "<span data-role=\"downloadCounter\"></span> seconds</span>" , $content );
$last = explode("' class='ipsButton ipsButton_primary ipsButton_small'" , $first[1] );
echo $last[0];
出什么问题了?
答案 0 :(得分:0)
您正在使用带有分隔符的explode,因此$last[0]
的值将为您提供一个字符串:
<a href='https://example.com/forum/files/file/2-content/?do=download&r=24050&confirm=1&t=1&csrfKey=72c4d0ffcabb34c6d6c490cbacbc354ag1536355261
您可以做的是利用DOMDocument和DOMXPath并指定类名以获取a标记,并从中获取href属性。然后使用parse_url和parse_str获取querystring参数。例如,如果您想获取csrfKey
:
$doc = new DOMDocument();
$doc->loadHTML($content);
$xpath = new DomXPath($doc);
$results = $xpath->query("//a[contains(@class, 'ipsButton') and contains(@class, 'ipsButton_primary') and contains(@class, 'ipsButton_small')]");
foreach ($results as $result) {
$url = parse_url($result->getAttribute("href"), PHP_URL_QUERY);
parse_str($url, $params);
echo $params['csrfKey'];
}