PHP爆炸未定义的偏移量通知

时间:2018-09-08 08:32:29

标签: php explode notice e-notices

我正在尝试获取令牌链接,但我不能。来源:

<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&amp;r=24050&amp;confirm=1&amp;t=1&amp;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];

出什么问题了?

1 个答案:

答案 0 :(得分:0)

您正在使用带有分隔符的explode,因此$last[0]的值将为您提供一个字符串:

<a href='https://example.com/forum/files/file/2-content/?do=download&amp;r=24050&amp;confirm=1&amp;t=1&amp;csrfKey=72c4d0ffcabb34c6d6c490cbacbc354ag1536355261

您可以做的是利用DOMDocumentDOMXPath并指定类名以获取a标记,并从中获取href属性。然后使用parse_urlparse_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'];
}

Demo