0

我希望通过 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"
            }
        }
...
    ]
}
}
4

0 回答 0