2017-05-16 18 views
0

私は私のスクリプトを実行すると、私は次のようなエラーメッセージが出ます:未定義のプロパティ:stdclassですが、プロパティは存在しますか?

PHP Notice: Undefined property: stdClass::$sub in /path/to/cron_monitor.php on line 14 

ライン14は、この(それは同様の関連だから、ライン13を含む)である:

$data=(object)$obj; 
$subject=$data->sub; 

グーグル/その他スタック質問プロパティが存在してはならないと私に伝えますが、私がこれを行うと(大きい部分から抜粋したが、$dataが4chan catalog.json APIから出力されている)、それが存在することを示している:

奇妙なことに10
object(stdClass)#2 (26) { 
    ["no"]=> 
    int(176833602) 
    ["now"]=> 
    string(21) "05/15/17(Mon)02:08:45" 
    ["name"]=> 
    string(9) "Anonymous" 
    ["sub"]=> 
    string(28) "/dfg/ Dwarf Fortress General" 
    ["com"]=> 
    string(2173) "(excluded since it just bloats up the question, the string is correct)" 
    ["filename"]=> 
    string(12) "Human Farmer" 
    ["ext"]=> 
    string(4) ".jpg" 
    ["w"]=> 
    int(1530) 
    ["h"]=> 
    int(1027) 
    ["tn_w"]=> 
    int(250) 
    ["tn_h"]=> 
    int(167) 
    ["tim"]=> 
    float(1494828525011) 
    ["time"]=> 
    int(1494828525) 
    ["md5"]=> 
    string(24) "0tPuwatHh8Kq/xHEEaWR2Q==" 
    ["fsize"]=> 
    int(1205673) 
    ["resto"]=> 
    int(0) 
    ["bumplimit"]=> 
    int(0) 
    ["imagelimit"]=> 
    int(0) 
    ["semantic_url"]=> 
    string(26) "dfg-dwarf-fortress-general" 
    ["custom_spoiler"]=> 
    int(1) 
    ["replies"]=> 
    int(435) 
    ["images"]=> 
    int(113) 
    ["omitted_posts"]=> 
    int(432) 
    ["omitted_images"]=> 
    int(110) 
    ["last_replies"]=> 
    array(3) { 
    [0]=> 
    array(6) { 
     ["no"]=> 
     int(176969172) 
     ["now"]=> 
     string(21) "05/16/17(Tue)13:14:50" 
     ["name"]=> 
     string(9) "Anonymous" 
     ["com"]=> 
     string(171) "(excluded since it just bloats up the question, the string is correct)" 
     ["time"]=> 
     int(1494954890) 
     ["resto"]=> 
     int(176833602) 
    } 
    [1]=> 
    array(6) { 
     ["no"]=> 
     int(176969476) 
     ["now"]=> 
     string(21) "05/16/17(Tue)13:18:23" 
     ["name"]=> 
     string(9) "Anonymous" 
     ["com"]=> 
     string(124) "(excluded since it just bloats up the question, the string is correct)" 
     ["time"]=> 
     int(1494955103) 
     ["resto"]=> 
     int(176833602) 
    } 
    [2]=> 
    array(6) { 
     ["no"]=> 
     int(176969731) 
     ["now"]=> 
     string(21) "05/16/17(Tue)13:21:20" 
     ["name"]=> 
     string(9) "Anonymous" 
     ["com"]=> 
     string(179) "(excluded since it just bloats up the question, the string is correct)" 
     ["time"]=> 
     int(1494955280) 
     ["resto"]=> 
     int(176833602) 
    } 
    } 
    ["last_modified"]=> 
    int(1494955280) 
} 

、私はまた後で

$threadno=$data->no; 

ラインを持っており、それが有効な値(整数)を返しません。

EDIT:私の全体のコードブロック:あなたは、あなたのデータ構造

に十分な非常に深く進んでいないとsubは常に理由とすることができる、すべてのoccurancesに存在していないように見えます

<?php 
error_reporting(E_ALL); 
include "chan_archiver.php"; 
include "config.php"; 

class threadMonitor extends chan_archiver{ 
     function monitorCatalog($boardwatch, $filter, $basedescription) { 
       $json=json_decode(file_get_contents('http://a.4cdn.org/'.$boardwatch.'/catalog.json'),true); 
       $monitordescription=$basedescription.date(DATE_RFC850); 
       foreach($json as $thread){ 
         $arr=$thread['threads']; 
         foreach($arr as $obj){ 
           $data=(object)$obj; 
           var_dump($data); 
           $subject=$data->sub; 
           if (strpos($subject, $filter) !== false){ 
               $threadno=$data->no; 
               $this->addThread($threadno, $boardwatch, $monitordescription); 
           } 
         } 
       } 
     } 
} 
$t=new threadMonitor(); 

/* IMPORTANT: Add arguments like this: 
    board filter basedescription 
    Example crontab command (that last space is needed, the command automatically adds the added date and time to the thread): 
     php /path/to/cron_monitor.php vg "/dfg/" "Dwarf Fortress General - " 
*/ 

$t->monitorCatalog($argv[1],$argv[2],$argv[3]); 
?> 
+0

を取得しているあなたは 'のvar_dump()は'も少しOを見えること試してみて、オブジェクト – RiggsFolly

+0

にキャストする必要性を感じる前に、だから、 '$ obj'ものですプロパティの可視性はプロパティの 'public/protected/private'のように記述されていないので、dd? – RiggsFolly

+0

@RiggsFolly - コードブロックを投稿に添付しました。たぶん私は間違ったことをしているのかもしれません。私が真剣にPHPを使って何かをしようとしているのは初めてです。だから何かがこれを引き起こすどこかで間違っているかもしれません。 – Ev1l0rd

答えて

0

あなたはエラーに

foreach($json as $obj){ 

    foreach ($obj->threads as $thread) { 
     echo $thread->no . PHP_EOL; 
     echo $thread->now . PHP_EOL; 
     if (isset($thread->sub)) { 
      echo $thread->sub . PHP_EOL; 
     } 
    } 

} 
+0

作業しましたが、 '$ obj'と' $ thread'の両方をオブジェクトとして定義しなければなりませんでした。そうでないとエラーが発生しました。私を助けてくれてありがとう。 – Ev1l0rd

+0

それは変です。私はあなたのJSONStringを使用し、私は – RiggsFolly

関連する問題