0

好的 在这里编辑仍然有问题,但与更新代码之前不同

<?php
    date_default_timezone_set('Europe/London');
    $date = date('Y-m-d H:i:s', time());
    $now = date('Y-m-d H:i:s');
    $dbhost     = "localhost";
    $dbuser     = "admin_cdm_clap";
    $dbpass     = "zdqUyFc5bAG1e6TWrIhijH3j7gB8xhmHI4q";
    $db         = "iTunesConnect";
    $driveID = 562220229;
    $connect = mysql_connect($dbhost,$dbuser,$dbpass) or die (mysql_error());
    mysql_select_db($db);
    $query = "SELECT `code` from `countries`";
    $result = mysql_query($query) or die (mysql_error());
    $num=mysql_num_rows($result);
    $i=0;
    while ($i < $num) {
        $country=mysql_result($result,$i,"code");
        $completeUrl = "https://itunes.apple.com/".strtolower($country)."/rss/toppaidapplications/limit=300/genre=6002/xml";
        //echo $completeUrl.'<br>';
        $xml = simplexml_load_file($completeUrl);
        $entries = $xml->entry;
        for ($a = 0; $a < 300; $a++) {  
            //$updated = $entries[$a]->updated;
            $id = $entries[$a]->id->attributes('im', true)->id;
            $bundle = $entries[$a]->id->attributes('im', true)->bundleId;
            $cc = $entries[$a]->id;
            $title = $entries[$a]->title;
            $category = $entries[$a]->category['term'];     
            $nUpdated = microtime(true); //strtotime(substr($updated,0,10).' '.substr($updated, 11, 8))
            $storeCode = substr($cc, 25, 2);
            $storeCat = 6002; 
            if($id == $driveID){
                $rankRec = "INSERT INTO rankings (id, rank, updated, store, category) VALUES (NULL, $a, $nUpdated, '$storeCode','$storeCat')";
                mysql_query($rankRec) or die(mysql_error());
            } else {
                error_reporting(E_ALL);
                ini_set('display_errors', '1');
            }   
        }       
        $i++;
    }   
?>

我现在收到以下错误;

注意:试图在第 24 行的 /var/www/vhosts/createdm.com/httpdocs/iTunesConnect/iTunesRanking.php 中获取非对象的属性 致命错误:在 / 中的非对象上调用成员函数 attributes()第 24 行的 var/www/vhosts/createdm.com/httpdocs/iTunesConnect/iTunesRanking.php

4

1 回答 1

1

错误在这里

 $id = $entries[$a]->id->attributes('im', true)->id;

它说该attributes函数是在一个非对象上调用的,这告诉我们该->id不是一个对象;它可能未设置。

所以这反过来告诉我们 XML 有一个没有正确的条目id,或者simplexml_load_file 认为有。

我会加强对对象的检查:

// This will save the current XML to a file, so that we may check
// the EXACT data we got, instead of requesting it again and, who
// knows?, get an intact copy, or a different version altogether
$dat = file_get_contents($completeUrl);
$fp  = fopen('temp.xml', 'w');
fwrite($fp, $dat);
fclose($fp);
$xml = simplexml_load_file('temp.xml');

// Then...
$entries = $xml->entry;
for ($a = 0; $a < 300; $a++) {
    if (!isset($entries[$a]))
        die("Entries $a-300 were not found");
    // if you discover that some small country has not 300 entries,
    // then: (a) reactivate the error reporting which should have warned you,
    // (b) replace the 300 with count($entries), or the for with a foreach loop.

    // Moved $cc= here, it's more efficient.
    $cc = $entries[$a]->id;
    if (!is_object($cc))
        die("Check entry $a's 'id' in temp.xml");
    $id  = $cc->attributes('im', true)->id;

    $bundle = $cc->attributes('im', true)->bundleId;
    // $cc = $entries[$a]->id;
    $title = $entries[$a]->title;
于 2012-10-16T15:48:47.627 回答