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.

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

报告相同问题?

悬赏问题

  • ¥15 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 采用的你的方案
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败