有人可以查看下面的脚本,为什么不列出wonder(< div>< / div>)的内容,即使地址是正确的。
<?php
function get_content($url)
{
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, 0);
ob_start();
curl_exec ($ch);
curl_close ($ch);
$string = ob_get_contents();
ob_end_clean();
$divs = '/<div>(.+)<\/div>/U';
preg_match($divs, $string, $matches);
$vysledek = $matches[1];
var_dump($vysledek);
var_dump($url);
}
echo get_content ("http://www.azlyrics.com/lyrics/garthbrooks/midnightsun.html");
?>
var_dump我写道: 空值 字符串(59)&#34; http://www.azlyrics.com/lyrics/garthbrooks/midnightsun.html&#34;
早些时候我用过:
function ziskatlyrics($url)
{
$content = file_get_contents($url);
$first_step = explode( '<div>' , $content );
$second_step = explode("</div>" , $first_step[1] );
$obsah = strip_tags($second_step[0]);
return $obsah;
}
echo ziskatlyrics("http://www.azlyrics.com/lyrics/garthbrooks/midnightsun.html");
但是它引发了我这个错误: 警告:file_get_contents( http://www.azlyrics.com/...ightsun.html ):无法打开流:HTTP请求失败!在
我可以做点什么让它再次发挥作用? 谢谢你的回答。
答案 0 :(得分:0)
因为你只是var_dump()
个变量,而不是curl请求的响应......
var_dump($vysledek);
var_dump($url);
你想要的是将curl响应分配给变量并返回...
$response = curl_exec($ch);
return $response;
嗯......在你完成了正在进行的正则表达式后,我认为你的$string
是空的。
答案 1 :(得分:0)
我不知道我是否如此正确:
function get_content($url)
{
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, true);
ob_start();
curl_exec ($ch);
curl_close ($ch);
$string = curl_exec ($ch);
ob_end_clean();
$divs = '/<div>(.+)<\/div>/U';
preg_match($divs, $string, $matches);
$vysledek = $matches[1];
return $vysledek;
}
echo get_content ('http://www.azlyrics.com/lyrics/garthbrooks/midnightsun.html');
结果:link