1

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

我有以下 URL,但这也为用户带来了正常的帖子。

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

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

4

1 回答 1

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

            }

    } ?>
于 2013-03-06T17:51:03.960 回答