仅获取用户的Twitter照片流

时间:2013-01-23 16:41:12

标签: twitter

我正在尝试以用户发布到Twitter的所有照片的JSON格式列表。

我有以下网址,但这也会为用户带来正常的帖子。

https://api.twitter.com/1/statuses/user_timeline/adrianfraguela.json?include_entities=t&count=1&callback=?

我怎样才能获得他们的照片?

1 个答案:

答案 0 :(得分:0)

我设法使用以下PHP完成此操作。我已经为大多数行添加了注释,因此很容易理解。

<?php
//setting up variables to use with 
    $twitterUsername = 'someUser';
    $jsonURL = 'https://api.twitter.com/1/statuses/user_timeline/'.$twitterUsername.'.json?include_entities=t&count=50&callback=?';
    $json = file_get_contents($jsonURL, true); //getting the file content           
    $decode = json_decode($json, true); //create array of contents
    $counter = 0; //set up a counter so we can count the number of iterations
    $howMany = 7; //how many images we would like to return

    foreach($decode as $val) {
        $counter ++;
        $checkMedia = $val["entities"]["media"][0]; //select the branch we want to work with

            if ((is_array($checkMedia)) && array_key_exists("media_url", $checkMedia)) { //check to see if the array has a media url if so continue
                $url = $checkMedia["media_url"];//url of the image
                $picText = $val["text"];//text to go with the image

                $picText = strip_tags($picText); ?>

                <a href="<?php echo $url; ?>"><img src="<?php echo $url; ?>" alt="<?php echo $picText; ?>" title="<?php echo $picText; ?>" /></a>

                <?php if ($counter == $howMany) {break;} //break out of loop after x iterations

            }

    } ?>