dongzhang7157 2011-07-05 09:03
浏览 30
已采纳

PHP / Wordpress - 将WordPress功能与现有的PHP代码集成在一起

I have the following PHP code that I am using to pull Tweets:

<?php
class TwitterFeed {
    public $tweets = array();
    public function __construct($user, $limit = 5) {
        $user = str_replace(' OR ', '%20OR%20', $user);
        $feed = curl_init('http://search.twitter.com/search.atom?q=from:'. $user .'&rpp='. $limit);
        curl_setopt($feed, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($feed, CURLOPT_HEADER, 0);
        $xml = curl_exec($feed);
        curl_close($feed);
        $result = new SimpleXMLElement($xml);
        foreach($result->entry as $entry) {
            $tweet = new stdClass();
            $tweet->id = (string) $entry->id;
            $user = explode(' ', $entry->author->name);
            $tweet->user = (string) $user[0];
            $tweet->author = (string) substr($entry->author->name, strlen($user[0])+2, -1);
            $tweet->title = (string) $entry->title;
            $tweet->content = (string) $entry->content;
            $tweet->updated = (int) strtotime($entry->updated);
            $tweet->permalink = (string) $entry->link[0]->attributes()->href;
            $tweet->avatar = (string) $entry->link[1]->attributes()->href;
            array_push($this->tweets, $tweet);
        }
        unset($feed, $xml, $result, $tweet);
    }
    public function getTweets() { return $this->tweets; }
}
$feed = new TwitterFeed('trekradio', 4);
$tweets = $feed->getTweets();

?>

I need to replace 'trekradio' in this line $feed = new TwitterFeed('trekradio', 4); so that it uses the following code. This code defines the Twitter ID on a Dynamic Basis from a Database Field.

<?php if (have_posts()) { $flag = true;  while (have_posts()) { the_post();
if ($flag) { $value = get_cimyFieldValue(get_the_author_ID(), 'twitter-username');
if ($value != NULL) echo . cimy_uef_sanitize_content($value) . ;
$flag = false;    }}} ?>

Can someone show me how to integrate the code so that it will properly function?

Thanks

  • 写回答

1条回答 默认 最新

  • douzhan8395 2011-07-05 09:35
    关注

    The assumption I am making is that $value is the variable you will want to send through as the parameter?

    I'm also assuming that the TwitterFeed class is accessible from where you are trying to run the second snippet of code.

    The final assumption is that cimy_uef_sanitize_content returns a value that is valid to be passed as the parameter to the TwitterFeed constructor.

    Remove the $feed and $tweets line from below the class (unless you explicitly require it to be run against trekradio).

    Then use the following code:

    <?php 
    if (have_posts()) { 
        $flag = true;  
        while (have_posts()) { 
            the_post();
            if ($flag) { 
                $value = get_cimyFieldValue(get_the_author_ID(), 'twitter-username');
                if ($value != NULL) {
                    $feed = new TwitterFeed(cimy_uef_sanitize_content($value), 4);
                    $tweets = $feed->getTweets();
                    // Do whatever you need to do with your Tweets here
                }
                $flag = false;    
            }
        }
    } ?>
    

    But why are you asking this question again? It was surely already answered in PHP - An Echo inside of an existing function

    EDIT: Question: What is the difference between 'twitter-username' and the value you want to send to the $user parameter in your TwitterFeed constructor? If these are the same it could be simplified.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP