我正在尝试使用 curl 和 DOMDocument 提取信息,我必须提取所有链接的 div 层。
但没有向我展示任何东西,我不明白,因为如果它没有 curl 的话。
function media_uri_request($url, $method='', $vars='')
{
$ch = curl_init();
if ($method == 'post')
{
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $vars);
}
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, false);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_FAILONERROR, false);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
curl_setopt ($ch, CURLOPT_HTTPHEADER, array("REMOTE_ADDR: ".$_SERVER['REMOTE_ADDR'], "HTTP_X_FORWARDED_FOR: ".$_SERVER['REMOTE_ADDR']));
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'tmp/cookie.txt');
curl_setopt ($ch, CURLOPT_COOKIEFILE, 'tmp/cookie.txt');
curl_setopt ($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt ($ch, CURLOPT_TIMEOUT, 0);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);
$buffer = curl_exec($ch);
curl_close ($ch);
if (isset($buffer) && filter_var($buffer, FILTER_SANITIZE_URL)) {
$urls = Array();
$dom = new DOMDocument();
@$dom->loadHTMLFile($buffer);
foreach($dom->getElementsByTagName('a') as $buffer) {
$urls[] = Array(
'name' => $buffer->nodeValue,
'href' => $buffer->getAttribute('href'),
'title' => $buffer->getAttribute('title'),
'rel' => $buffer->getAttribute('rel'),
'id' => $buffer->getAttribute('id'),
);
}
return $urls;
}
}
目前向我显示页面上的所有链接,但我只需要一个 div 的 id 并获取此链接。
<div id="something">
<a href="anylink">sometitle</a>
<a href="anylink">sometitle</a>
<a href="anylink">sometitle</a>
<a href="anylink">sometitle</a>
</div>
你能帮我吗?