整合功能

时间:2010-08-28 23:33:35

标签: function integrate

这是我关于获取div内容的问题的后续行动。第二个功能是我很难用的。我很确定我可以从另一个调用一个函数,但我不确定如何将它们放在另一个像我在这里做的那样。它显然是一个使代码工作的愚蠢尝试,因为它给了我一个错误:

Blackline Frostbyte:有货。 :$ 139.99

致命错误:无法重新声明第69行/home/rambazar/public_html/cron.php中的get_string_between()(先前在/home/rambazar/public_html/cron.php:69中声明) < / p>

正如我所看到的,代码部分正常,因为它获得了产品库存信息和价格标签,但代码停止了,我无法弄清楚 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;
}
?>

1 个答案:

答案 0 :(得分:1)

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;
}