2012-12-10 1 views
1

여러 피드의 마지막 몇 개 트윗을 표시하는 사용자 정의 트위터 위젯을 만들려고했습니다.Wordpress get_transient() 배열 대신 인코딩 된 문자열을 반환하는 경우

<?php 

class Foo{ 
    protected $name = ''; 

    //opts 
    protected $numTweets = 3; 
    protected $transName = ''; // Name of value in database. 
    protected $backupName = ''; 
    protected $cacheTime = 5; // Time in minutes between updates. 
    protected $exclude_replies = true; // Leave out @replies? 

    public function __construct($username){ 
    $this->name = $username; 
    $this->transName = 'jctft3' . $username; 
    $this->backupName = 'jctfb3' . $username; 
    } 

    public function get_feed(){ 
    $feed = get_transient($this->transName); 

    // Do we already have saved tweet data? If not, lets get it. 
    if($feed === false ){ 
     $feed = $this->call_twitter(); 
    } 

    $html = $this->build_html($feed); 

    return $html; 
    }// protected function get_feed(){...} 

    protected function call_twitter(){ 
    /* We need the following: 

    $feed = array(
     'username' => '', 
     'friendly_name' => '', 
     'url' => '' 

     'tweets' => array(
     array(
      'html' => '', 
      'url' => '', 
      'time' => '' 
     ), 
     array(
      'html' => '', 
      'url' => '', 
      'time' => '' 
     ) 
    ) 
    ); 
    */ 
    $feed = array(); 

    // Get the tweets from Twitter. 
    $response = wp_remote_get("http://api.twitter.com/1/statuses/user_timeline.json?screen_name={$this->name}&count=10&exclude_replies=false"); 

    // parse Twitter response into an array 
    if(!is_wp_error($response) && $response[ 'response' ][ 'code' ] == 200){ 
     // Get tweets into an array. 
     $tweets_json = json_decode($response['body'], true); 
     // Now update the array to store just what we need. 
     $feed[ 'username' ] = $tweets_json[ 0 ][ 'user' ][ 'screen_name' ]; 
     $feed[ 'friendly_name' ] = $tweets_json[ 0 ][ 'user' ][ 'name' ]; 
     $feed[ 'url' ] = 'https://twitter.com/' . $tweets_json[ 0 ][ 'user' ][ 'screen_name' ]; 
     $feed[ 'tweets' ] = array(); 

     foreach ($tweets_json as $tweet){ 
     $my_tweet = array(); 

     // Core info. 
     $my_tweet[ 'url' ] = 'http://twitter.com/#!/'. $feed[ 'username' ] .'/status/'. $tweet['id_str']; 
     // Message. Convert links to real links. 
     $pattern = '/http:(\S)+/'; 
     $replace = '<a href="${0}" target="_blank" rel="nofollow">${0}</a>'; 
     $my_tweet[ 'html' ] = preg_replace($pattern, $replace, $tweet['text']); 
     //$my_tweet[ 'html' ] = $tweet['text']; 
     // Need to get time in Unix format. 
     $time = $this->twitter_time($tweet[ 'created_at' ]); 
     //$time = date_parse($time); 
     //$uTime = mktime($time['hour'], $time['minute'], $time['second'], $time['month'], $time['day'], $time['year']); 
     $my_tweet[ 'time' ] = $time; 

     array_push($feed[ 'tweets' ], $my_tweet); 
     } 

     // Save our new transient, and update the backup. 
     set_transient($this->transName, $feed, 60 * $this->cacheTime); 
     update_option($this->backupName, $feed); 
    } 
    else{ 
     // i.e. Fetching new tweets failed. 
     $feed = get_option($this->backupName); // False if there has never been data saved. 
    } 

    return $feed; 
    } 

    protected function build_html($feed){ 
    $html = ''; 

    $is_top = false; 
    if($feed[ 'friendly_name' ] == 'Johnny Cupcakes'){ 
     $is_top = true; 
    } 

    ob_start(); 
    include( plugin_dir_path(__FILE__) . '../html/feed.html'); 
    $html = ob_get_clean(); 

    return $html; 
    } 

} 

?> 

플러그인은 HTML을 (데이터가 우리의 로컬 데이터베이스에 저장된 데이터에서 직접 트위터에서하지 당겨지는 경우 즉,) 빌드 처음으로, 모든 각 피드는 다음과 같은 클래스의 인스턴스를 생성 잘 표시됩니다. 의

Warning: Illegal string offset 'username' in /Applications/MAMP/htdocs/sites/wp-johnnycupcakes/wp-content/plugins/jc-twitter-widget/html/feed.html on line 12 

행 12 : 나는 get_transient를 사용할 때,() 나는 다음과 같은 오류가 (트윗이 오류를 생성하고 함수를 거부 얼마나 많은 트윗 변화하는) 트윗의 일부에, 캐시 된 데이터를 검색 할 feed.html은 다음과 같습니다

추가 조사시
<h3><a target="_new" href="<?php echo $feed[ 'url' ]; ?>"> @<?php echo $feed[ 'username' ]; ?> </a></h3> 

, 내가 '어떤 (나는() get_transient를 사용할 때 내 피드의 일부에, 반환 값이 더 이상 배열입니다 때로는 것처럼 보이지만, 인코딩 된 문자열 m은 데이터베이스에 transient_values를 저장하기위한 Wordpress의 인코딩 규칙이라고 가정합니다). 문자열은 다음과 같습니다

배열이 반환된다 (따라서 피드가 성공적으로 표시됩니다)
string(830) "a:4:{s:8:"username";s:15:"JHNYCPKS_London";s:13:"friendly_name";s:18:"Johnny Cupcakes UK";s:3:"url";s:35:"https://twitter.com/JHNYCPKS_London";s:6:"tweets";a:9:{i:0;a:3:{s:3:"url";s:63:"http://twitter.com/#!/JHNYCPKS_London/status/278196905350164480";s:4:"html";s:128:"Baked in USA long sleeve, Truffle and White Mens Basic Tee's are now back in stock come and grab them now while supplies last!!!";s:4:"time";s:11:"2 hours ago";}i:1;a:3:{s:3:"url";s:63:"http://twitter.com/#!/JHNYCPKS_London/status/278180246967173121";s:4:"html";s:121:"Put a pin on it! @ Johnny Cupcakes http://t.co/nxVSAG63";s:4:"time";s:11:"4 hours ago";}i:2;a:3:{s:3:"url";s:63:"http://twitter.com/#!/JHNYCPKS_London/status/278179975474053120";s:4:"html";s:161:"Big kid magnet came in today! " 

, 그것은 다음과 같습니다

array(4) { 
    ["username"]=> 
    string(15) "JHNYCPKS_Boston" 
    ["friendly_name"]=> 
    string(20) "JohnnyCupcakesBoston" 
    ["url"]=> 
    string(35) "https://twitter.com/JHNYCPKS_Boston" 
    ["tweets"]=> 
    array(7) { 
    [0]=> 
    array(3) { 
     ["url"]=> 
     string(63) "http://twitter.com/#!/JHNYCPKS_Boston/status/278204808756862976" 
     ["html"]=> 
     string(139) "IT'S RAINING! IT'S RAINING! IT'S RAINING! IT'S RAINING! IT'S RAINING! IT'S RAINING! IT'S RAINING! IT'S RAINING! IT'S RAINING! IT'S RAINING!" 
     ["time"]=> 
     string(11) "2 hours ago" 
    } 
    [1]=> 
    array(3) { 
     ["url"]=> 
     string(63) "http://twitter.com/#!/JHNYCPKS_Boston/status/278169219160489984" 
     ["html"]=> 
     string(167) "Four #freshlybaked keychains were released this weekend! Which is your favorite? <a href="http://t.co/Xn16NsjU" target="_blank" rel="nofollow">http://t.co/Xn16NsjU</a>" 
     ["time"]=> 
     string(11) "4 hours ago" 
    } 
    [2]=> 
    array(3) { 
     ["url"]=> 
     string(63) "http://twitter.com/#!/JHNYCPKS_Boston/status/278137791391617025" 
     ["html"]=> 
     string(29) "@thatkidTHILL good luck, man!" 
     ["time"]=> 
     string(11) "6 hours ago" 
    } 
    [3]=> 
    array(3) { 
     ["url"]=> 
     string(63) "http://twitter.com/#!/JHNYCPKS_Boston/status/278137703281881088" 
     ["html"]=> 
     string(96) "@dcachopa18 i don't think so, sorry! we'll let you know if we do. what size are you looking for?" 
     ["time"]=> 
     string(11) "6 hours ago" 
    } 
    [4]=> 
    array(3) { 
     ["url"]=> 
     string(63) "http://twitter.com/#!/JHNYCPKS_Boston/status/277888036237373440" 
     ["html"]=> 
     string(188) "Only a few hours left! Spend $50, get a $10 gift card. Stop in or shop online. We close at 7 tonight! <a href="http://t.co/082T5jTf" target="_blank" rel="nofollow">http://t.co/082T5jTf</a>" 
     ["time"]=> 
     string(12) "23 hours ago" 
    } 
    [5]=> 
    array(3) { 
     ["url"]=> 
     string(63) "http://twitter.com/#!/JHNYCPKS_Boston/status/277786268274933760" 
     ["html"]=> 
     string(135) "Here's the new Boston in-store! cc: @b3asTTw33ts <a href="http://t.co/soLoEYqv" target="_blank" rel="nofollow">http://t.co/soLoEYqv</a>" 
     ["time"]=> 
     string(9) "yesterday" 
    } 
    [6]=> 
    array(3) { 
     ["url"]=> 
     string(63) "http://twitter.com/#!/JHNYCPKS_Boston/status/277526406076968961" 
     ["html"]=> 
     string(119) "What's everyone's favorite piece from this release?! The new Boston exclusive seems to be the hot ticket. #freshlybaked" 
     ["time"]=> 
     string(9) "yesterday" 
    } 
    } 
} 

나는 원인이 될 수 있는지에 대한 몇 가지 아이디어를 가지고 이 : 당겨 점점 과도

  • 일부 캐릭터에 너무 많은 데이터를 저장하려고

    • 어느 방법 Wordpre를 어지럽히 ss는 데이터를 문자열로 또는 그 반대로 변환합니다.

    누구나이 문제에 경험이 있으며 올바른 방향으로 수정할 수 있습니까?

  • 답변

    5

    배열의 serialisation 일이 잘못되었습니다. 표시하는 샘플 코드화 된 문자열은 배열의 부분 직렬화 일 뿐이므로 직렬화 또는 DB 저장 중 어느 곳에서나 빠져 나간다.

    어쩌면 직접 serialize (serialize())하고 base64-encode으로 전화를 걸어 일시적으로 설정해보십시오. 검색시 base64-decodedeserialise 직렬화 된 배열을 저장하는 WordPress/wpdb에 문제가있는 경우 작동 할 수 있습니다.

    +1

    또 다른 가능한 해결 방법은 [맨 페이지의이 주석] (http://php.net/manual/en/function.serialize.php#)에 따라 검색시 json_encode() 및 json_decode()를 사용하는 것입니다. 92284). – webaware

    +2

    당신은 그것을 흔들어 움직입니다. 내 데이터를 저장하기 위해 serialize(), base64_encode(), set_transient()를 사용했습니다. 검색하려면 get_transient(), base64_decode(), unseralize()를 사용했습니다. –