【问题标题】:integrating a function整合一个功能
【发布时间】:2010-08-28 23:33:35
【问题描述】:

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

Blackline Frostbyte:有货。 : $139.99

致命错误:无法在第 69 行的 /home/rambazar/public_html/cron.php 中重新声明 get_string_between()(之前在 /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;
}
?>

【问题讨论】:

  • 此代码是cron.php 文件吗?
  • 是的,这是 cron 文件本身

标签: function integrate


【解决方案1】:

getprice() 函数中取出get_string_between(),你应该很高兴:

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

【讨论】:

    猜你喜欢
    • 2016-03-06
    • 2019-07-13
    • 1970-01-01
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    相关资源
    最近更新 更多