2010-08-28 182 views
0

這是我關於獲取div內容的後續問題。第二個功能是一個即時通訊。即時通訊很確定我可以從另一個調用函數,但我不知道如何將它們放入另一個,就像我在這裏做的那樣。它顯然是一個愚蠢的嘗試,使代碼工作,因爲它給了我一個錯誤:集成函數

黑線弗羅斯特:有貨。 :$ 139.99

致命錯誤:不能重新聲明get_string_between()(以前在/home/rambazar/public_html/cron.php:69聲明)在/home/rambazar/public_html/cron.php在線69

因爲我看到這一點,代碼是部分好的,因爲它獲取產品庫存信息和價格標籤,但代碼停止,我不知道哪裏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; 
} 
?> 
+0

這是代碼'cron.php'文件? – 2010-08-29 00:21:30

+0

是的,這是cron文件本身 – crashtest 2010-08-29 12:49:51

回答

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