这是对我关于获得div内容的问题的跟进。第二个函数是我很难处理的函数。我很确定我可以从另一个函数中调用一个函数,但我不确定是否像这里那样把它们放在一起。显然,这是一次愚蠢的尝试,试图使代码正常工作,因为它给了我一个错误:
黑线霜字节:库存。*139.99美元
致命错误:无法在第69行/home/rambazar/public_html/cron.php :69中重新声明get_string_between() (以前在/home/rambazar/public_html/cron.php中声明)
正如我所看到的,代码部分没有问题,因为它得到了产品库存信息和价格标签,但是代码停止了,我不知道get_string_between是在哪里重新声明的,因为它只被调用。请帮我整理一下,谢谢!
<?php
set_time_limit(1800);
include("admin/include/db.php");
error_reporting(E_ALL);
$res=mysql_query("select * from products");
while($row=mysql_fetch_array($res))
{
$availability=getavailability($row['newegg_productid']);
$price=getprice($row['newegg_productid']);
echo $row['productname']." : ".$availability." : ".$price."<br />";
}
function getavailability($itemId)
{
$url=trim("http://www.newegg.com/Product/Product.aspx?Item=".$itemId);
$ch = curl_init();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$content = curl_exec ($ch);
curl_close ($ch);
$content=strtolower($content);
$buff=$content;
$isAvailable=false;
$pos1=strpos($content,'<p class="note">')+16;
if($pos1==16)return "";
$pos2=strpos($content,'</p>',$pos1);
$availability= trim(substr($content,$pos1,($pos2-$pos1)));
return strip_tags($availability);
}
function getprice($itemId)
{
function get_string_between($string, $start, $end)
{
$string = " ".$string;
$ini = strpos($string,$start);
if ($ini == 0)
return "";
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
}
$data = file_get_contents("http://www.newegg.com/Product/Product.aspx?Item=".$itemId);
$pricediv = get_string_between($data, '<div class="current" id="singleFinalPrice"><span class="label">Now:', '</div');
$price = strip_tags($pricediv);
return $price;
}
?>发布于 2010-08-29 03:46:59
将get_string_between()从getprice()函数中取出,您应该可以这样做:
function get_string_between($string, $start, $end)
{
$string = " ".$string;
$ini = strpos($string,$start);
if ($ini == 0)
return "";
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
}
function getprice($itemId)
{
$data = file_get_contents("http://www.newegg.com/Product/Product.aspx?Item=".$itemId);
$pricediv = get_string_between($data, '<div class="current" id="singleFinalPrice"><span class="label">Now:', '</div');
$price = strip_tags($pricediv);
return $price;
}https://stackoverflow.com/questions/3592916
复制相似问题