views:

41

answers:

1

hi

this is the follow-up to my question about getting a div's content. the second function is the one im having a hard time with. im pretty sure i can call a function from another one, but im not sure about placing them into oneanother like i did here. its obviously a silly attempt to make the code work, since it gives me an error:

Blackline Frostbyte : in stock. : $139.99

*Fatal error: Cannot redeclare get_string_between() (previously declared in /home/rambazar/public_html/cron.php:69) in /home/rambazar/public_html/cron.php on line 69*

as i see this, the code is partially ok, because it gets a products stock info and the price tag, but the code stops and i cant figure out where *get_string_between* is redeclared, as it is only called. please help me sorting this out, thanks!

<?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  A: 

Take out the get_string_between() out of the getprice() function and you should be good to go:

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