0

次のコードを使用して、Twitter フィードを取得しようとしています。

// Make the request and get the response into the $json variable
$json =  $twitter->setGetfield($getfield)
                 ->buildOauth($url, $requestMethod)
                 ->performRequest();

// It's json, so decode it into an array
$result = json_decode($json);

// Access the profile_image_url element in the array
echo $result->created_at;
?>

次の結果が得られます。

Thu Oct 25 18:40:50 +0000 2012

テキストを取得しようとすると:

echo $result->text;

次のエラーが表示されます。

Notice: Undefined property: stdClass::$text in /Library/WebServer/Documents/include/twitter_noformat/items.php on line 35 

私のデータ形式の部分的な var_dump には、次のものが含まれます。

{"created_at":"Thu Aug 01 16:12:18 +0000 2013",
"id":362969042497175553,
"id_str":"362969042497175553",
"text":"A warm welcome to our new international students from China, Hong Kong and Germany! http:\/\/t.co\/GLvt3GynJV",
"source":"web",
"truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":

私の質問は次のとおりです。created_at は私に値を与えます。id は私に値を与えます。なぜテキストしないのですか?JSONについては何も知りません。私は高度なプログラマーではありませんが、パターンは同じように見えます。

編集: Twitter 配列を読みやすいものに変換するクールなスニペットを見つけました。関数は次のようになります。

// It's json, so decode it into an array
$result = json_decode($json);

// Access the profile_image_url element in the array
$pretty = function($v='',$c="&nbsp;&nbsp;&nbsp;&nbsp;",$in=-1,$k=null)use(&$pretty){$r='';if(in_array(gettype($v),array('object','array'))){$r.=($in!=-1?str_repeat($c,$in):'').(is_null($k)?'':"$k: ").'<br>';foreach($v as $sk=>$vl){$r.=$pretty($vl,$c,$in+1,$sk).'<br>';}}else{$r.=($in!=-1?str_repeat($c,$in):'').(is_null($k)?'':"$k: ").(is_null($v)?'&lt;NULL&gt;':"<strong>$v</strong>");}return$r;};

echo $pretty($result);

結果は次のようになります。

statuses_count: 583
lang: en
status:
    created_at: Thu Aug 01 21:10:10 +0000 2013
    id: 363044004444651522
    id_str: 363044004444651522
    text: @CalStateEastBay AD Sara Lillevand Judd '86 honored for her work as an athletic adminstrator. http://t.co/WzOqjIDrBw

テキストがオブジェクトの一部のように見えるので、これは奇妙ですか?

私は、twitter がオブジェクトの配列をキックバックすることを確認しました。これらのオブジェクトは多くのアイテム(?)を持つことができます。テキストではありません。それらは両方ともアレイの同じレベルにあります。

助けてくれてありがとう、ドノバン

4

1 に答える 1

1

さて、1日の調査の後の私の解決策は次のとおりです。

$result = json_decode( $json );
echo "Text:" . $result->status->text . "<br />";

テキストは status の子 (?) でした。配列の 2 つのレベルで使用されていたため、created_at をエコーすることができました。テキストは私が推測するステータスオブジェクトの中にありました。

created_at: Thu Oct 25 18:40:50 +0000 2012
favourites_count: 1
utc_offset: -25200
time_zone: Pacific Time (US & Canada)
geo_enabled: 1
verified:
statuses_count: 583
lang: en
status:
     created_at: Thu Aug 01 21:10:10 +0000 2013
     id: 363044004444651522
     id_str: 363044004444651522
     text: @CalStateEastBay AD Sara Lillevand Judd '86 honored for her work as an athletic adminstrator. http://t.co/WzOqjIDrBw
于 2013-08-02T15:09:07.207 に答える