执行后总是提示:Notice: Undefined offset: 1 in C:\WWW\get2.php on line 12
该怎么办呢?
我也用了$html = file_get_contents($url);
还是一样有时候可以出结果,有时候不能,为什么呢?<?php
require_once 'simple_html_dom.php';
$url = $_GET['url'];
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,10);
$html = curl_exec($ch);preg_match('/charset=\\\\"0000\\\\">(\d+)<\\\\\\/a><\\\\\\/li>\\\\n\\\\t\\\\t\\\\t<\\\\\\/ul>\\\\n<\\\\\\/div>\\\\n"}/',$html,$m);for($i=1;$i<=$m[1];$i++)
{
echo '</a>'.preg_replace('/"page":(\d+)}/',"\"page\":$i}",$url).'</b>';
}?>
该怎么办呢?
我也用了$html = file_get_contents($url);
还是一样有时候可以出结果,有时候不能,为什么呢?<?php
require_once 'simple_html_dom.php';
$url = $_GET['url'];
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,10);
$html = curl_exec($ch);preg_match('/charset=\\\\"0000\\\\">(\d+)<\\\\\\/a><\\\\\\/li>\\\\n\\\\t\\\\t\\\\t<\\\\\\/ul>\\\\n<\\\\\\/div>\\\\n"}/',$html,$m);for($i=1;$i<=$m[1];$i++)
{
echo '</a>'.preg_replace('/"page":(\d+)}/',"\"page\":$i}",$url).'</b>';
}?>
for($i=1;$i<=$m[1];$i++)
{
echo '</a>'.preg_replace('/"page":(\d+)}/',"\"page\":$i}",$url).'</b>';
}
谢谢斑竹的回答,回去测试了一下结果还是一样,提示错误是在12行,那个for循环,是不是for有问题呢?不过看起来没有啊,应该是数值没有获取到吧?也就是那个$m没有获取到有时候还会提示failed to open stream: HTTP request failed!
出现 Notice: Undefined offset: 1 是因为 preg_match 执行失败时 $m[1] 不存在
所以要做条件判断!
原来如此,谢谢斑竹提醒,我测试了很多次,发现跟获取内容有关系,
我用过:$html = file_get_contents($url);
也用过:curl都有时候会出现failed to open stream: HTTP request failed!
这个问题该怎么办呢?
未能打开流:HTTP请求失败!这个错误不会在 curl 出现你可以
$html = @file_get_contents($url);
屏蔽掉错误,但一定要检查 $html 是否为空
$url=preg_replace("/http:\/\/|https:\/\//is","",$url);