制作一个foreach到twitter api生成的json文件

时间:2012-12-23 17:50:13

标签: php twitter foreach json

我希望通过Twitter API生成我喜欢的推文的JSON文件。

我有一个正确显示推文的PHP脚本,但是当生成所有推文json文件时,我只有一条推文出现。

<?php

if(!empty($content)){foreach($content as $tweet){
    echo'
        <div class="twitter_status" id="'.$tweet->id_str.'">
            <div class="bloc_content">
                <p class="status tw_status">'.parseTweet($tweet->text).'</p>
            </div>
            <div class="bloc_caption">
                <a href="http://twitter.com/'.$tweet->user->screen_name.'">
                    <img src="'.$tweet->user->profile_image_url.'" alt="@'.$tweet->user->name.'" class="userimg tw_userimg"/>
                    <span class="username tw_username">@'.$tweet->user->screen_name.'</span>
                </a>
                <span class="timestamp tw_timestamp">'.date('d M / H:i',strtotime($tweet->created_at)).'</span>
            </div>
        </div>';
    }
}

/*
 * Transform Tweet plain text into clickable text
 */
function parseTweet($text) {
    $text = preg_replace('#http://[a-z0-9._/-]+#i', '<a  target="_blank" href="$0">$0</a>', $text); //Link
    $text = preg_replace('#@([a-z0-9_]+)#i', '@<a  target="_blank" href="http://twitter.com/$1">$1</a>', $text); //usernames
    $text = preg_replace('# \#([a-z0-9_-]+)#i', ' #<a target="_blank" href="http://search.twitter.com/search?q=%23$1">$1</a>', $text); //Hashtags
    $text = preg_replace('#https://[a-z0-9._/-]+#i', '<a  target="_blank" href="$0">$0</a>', $text); //Links
    return $text;
}

/*
 * Création du fichier json
 */

$named_array = array(
"timeline" => array(
"headline" => "Les gens",
"type" => "default",
"text" => "People say stuff",
"asset" => array(
    "media" => "http://www.lephpfacile.com/images/design/php.gif",
    "credit" => "Credit Name Goes Here",
    "caption" => "Caption text goes here",
),
"startDate" => "2012,1,1",
"date" => array(
    array(
        "startDate" => "2012,1,1",
        "endDate" => "2012,1,1",
        "headline" => "gzergzergergerg",
        "text" => "ergergergerg",
        "asset" => array(
        "media" => "https://twitter.com/".$tweet->user->screen_name."/status/".$tweet->id_str,
        "credit" => "gergergerg",
        "caption" => "ergergergerg"
    )
),
),
)
);

echo json_encode($named_array);

//6 - Generation du fichier .json
$fp = fopen('tweets.json', 'w');
fwrite($fp, json_encode($named_array));
fclose($fp);

?>

这是我的json文件的结果

{
"timeline": {
    "headline": "Les gens",
    "type": "default",
    "text": "People say stuff",
    "asset": {
        "media": "http://www.lephpfacile.com/images/design/php.gif",
        "credit": "Credit Name Goes Here",
        "caption": "Caption text goes here"
    },
    "startDate": "2012,1,1",
    "date": [
        {
            "startDate": "2012,1,1",
            "endDate": "2012,1,1",
            "headline": "gzergzergergerg",
            "text": "ergergergerg",
            "asset": {
                "media": "https://twitter.com/PierreDeruelle/status/174039730093170688",
                "credit": "gergergerg",
                "caption": "ergergergerg"
            }
        }
    ]
}
}

如何在此文件json中显示其他推文?

以下是我想要的json示例。

{
"timeline": {
    "headline": "Les gens",
    "type": "default",
    "text": "People say stuff",
    "asset": {
        "media": "http://www.lephpfacile.com/images/design/php.gif",
        "credit": "Credit Name Goes Here",
        "caption": "Caption text goes here"
    },
    "startDate": "2012,1,1",
    "date": [
        {
            "startDate": "2012,1,1",
            "endDate": "2012,1,1",
            "headline": "gzergzergergerg",
            "text": "ergergergerg",
            "asset": {
                "media": "https://twitter.com/PierreDeruelle/status/174039730093170688",
                "credit": "gergergerg",
                "caption": "ergergergerg"
            }
        }
 "startDate": "2012,1,1",
    "date": [
        {
            "startDate": "2012,1,1",
            "endDate": "2012,1,1",
            "headline": "gzergzergergerg",
            "text": "ergergergerg",
            "asset": {
                "media": "https://twitter.com/PierreDeruelle/status/174039730093170688",
                "credit": "gergergerg",
                "caption": "ergergergerg"
            }
        }
 "startDate": "2012,1,1",
    "date": [
        {
            "startDate": "2012,1,1",
            "endDate": "2012,1,1",
            "headline": "gzergzergergerg",
            "text": "ergergergerg",
            "asset": {
                "media": "https://twitter.com/PierreDeruelle/status/174039730093170688",
                "credit": "gergergerg",
                "caption": "ergergergerg"
            }
        }
 "startDate": "2012,1,1",
    "date": [
        {
            "startDate": "2012,1,1",
            "endDate": "2012,1,1",
            "headline": "gzergzergergerg",
            "text": "ergergergerg",
            "asset": {
                "media": "https://twitter.com/PierreDeruelle/status/174039730093170688",
                "credit": "gergergerg",
                "caption": "ergergergerg"
            }
        }
...
    ]
}
}

0 个答案:

没有答案