2010-08-28 20 views
0

c'est la suite à ma question sur l'obtention du contenu d'une div. la deuxième fonction est celle qui a du mal avec. Im assez sûr que je peux appeler une fonction d'un autre, mais je ne suis pas sûr de les placer dans un autre comme je l'ai fait ici. c'est évidemment une tentative idiote de faire fonctionner le code, car il me donne une erreur:l'intégration d'une fonction

Blackline Frostbyte: en stock. : 139,99 $

Erreur fatale: Impossible redéclarer get_string_between() (précédemment déclarée /home/rambazar/public_html/cron.php:69) dans /home/rambazar/public_html/cron.php sur la ligne 69

comme je le vois, le code est en partie ok, car il obtient une information sur les produits et le prix, mais le code s'arrête et je ne peux pas savoir où get_string_between est redéclaré, comme il est seulement appelé. s'il vous plaît aidez-moi à trier cela, merci!

<?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; 
} 
?> 
+0

Est-ce code le fichier 'cron.php'? –

+0

oui, ceci est le fichier cron lui-même – crashtest

Répondre

1

Sortez le get_string_between() de la fonction getprice() et vous devriez être bon d'aller:

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