简体   繁体   中英

DOMXPath query error , not working in web server

i am using the following code to grab some data from a site and store them in a database .

<?php

error_reporting(0);
include("database.php");

$dom = new DOMDocument();
$url = "http://www.kitco.com";
$html = file_get_contents($url);
$dom->loadHTML($html);
$xp = new DOMXPath($dom);
get_live_spot_gold();
get_silver_and_pgm();

function get_live_spot_gold()
{
    global $xp;
    $live_spot_gold = array();

    $bid='000';
    $change='000';
    $qs = $xp->query('//div[@class="item_border"]/table/tr[@class="alternating"][1]/td[2]');
    foreach($qs as $q)
    {
            $bid = $q->textContent;
            break;

    }



    $qs = $xp->query('//div[@class="item_border"]/table/tr[@class="alternating"][2]/td[2]');
    foreach($qs as $q)
    {
        $change = $q->textContent;
        break;
    }



    //insert into db
    echo $bid."<br>";
    echo $change."<br>";
    $query = "UPDATE live_spot_gold SET _bid = '$bid' , _change = '$change'";
    echo $query;
    $result = mysql_query($query);
    if(!$result)echo "problem in live spot gold"."<br>";



}




function get_silver_and_pgm()
{
    global $xp;
    $silver_and_pgm = array();
    $cnt=0;
    $qs = $xp->query('//td[@id="right_column"]/div[@class="item_container"][2]/div[@class="item_border"]/table/tr');
    foreach ($qs as $q)
    {
        $line = $q->nodeValue;
        $demo="";
        for($i=0;$i<strlen($line);$i++)
        {
            if($line[$i]==' ')
            {
                $demo.=' ';
                for(  ;$line[$i]==' ';$i++);
            }
            $demo.=$line[$i];

        }
        $words = explode(" ",$demo);
        $silver_and_pgm[$cnt][0]=$words[0];  //metal name
        $silver_and_pgm[$cnt][1]=$words[1];  //bid
        $silver_and_pgm[$cnt][2]=$words[2];  //change
        $cnt++;
    }

    for($i=0;$i<$cnt;$i++)
    {
        $metal_name = $silver_and_pgm[$i][0];
        $bid = $silver_and_pgm[$i][1];
        $change = $silver_and_pgm[$i][2];

        //echo "here";
        echo $metal_name."<br>";
        echo $bid."<br>";
        echo $change."<br>";


        //$query = "insert into 'silver_and_pgm' values('$metal_name','$bid','$change')";
        //$query = "UPDATE silver_and_pgm set _bid='$bid' WHERE _metal_name='$metal_name'";

        //$query = "UPDATE silver_and_pgm set _bid = '$bid' WHERE _metal_name = '$metal_name'";
        $query = "UPDATE silver_and_pgm set _bid='$bid',_change = '$change' WHERE _metal_name 
                    like'$metal_name%'";


        echo $query."<br>";
        $result = mysql_query($query);
        if(!$result)
            echo "problem in silver_and_pgm"."<br>";

    }




}



?>

its just working fine in my localhost . but not in my web server . Its grabbing data and storing them in localhost ... no query , no echo statement is working in web server. would anybody please suggest a way to handle this problem ??

DOM requires PHP 5 (and libxml to be installed). Make sure your server meets the requirements by running php_info(). Also, turn back on your error reporting and see what errors you get.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM