0

これは、divのコンテンツの取得に関する私の質問のフォローアップです。2番目の機能は、苦労している機能です。私は別の関数から関数を呼び出すことができると確信していますが、ここで行ったようにそれらを互いに配置することについては確信がありません。それは私にエラーを与えるので、明らかにコードを機能させるためのばかげた試みです:

Blackline Frostbyte:在庫あり。:$ 139.99

致命的なエラー:69行目の/home/rambazar/public_html/cron.phpでget_string_between()(以前は/home/rambazar/public_html/cron.php:69で宣言されていた)を再宣言できません

これを見ると、製品の在庫情報と値札を取得しているため、コードは部分的に問題ありませんが、コードが停止し、get_string_betweenが呼び出されるだけなので、どこで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;
}
?>
4

1 に答える 1

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;
}
于 2010-08-29T03:46:59.507 に答える