致命错误:不能使用stdClass类型的对象作为数组在.. functions.php

问题描述:

发现这个代码在线,它的工作,然后突然有一天,我得到“致命的错误:不能使用stdClass类型的对象作为数组。 。functions.php“在这一行$ count = absint($ json [0] - > total_count);致命错误:不能使用stdClass类型的对象作为数组在.. functions.php

function ds_post_like_count($post_id) { 

    // Check for transient 
    if (! ($count = get_transient('ds_post_like_count' . $post_id))) { 

    // Setup query arguments based on post permalink 
    $fql = "SELECT url, "; 
    //$fql .= "share_count, "; // total shares 
    //$fql .= "like_count, "; // total likes 
    //$fql .= "comment_count, "; // total comments 
    $fql .= "total_count "; // summed total of shares, likes, and comments (fastest query) 
    $fql .= "FROM link_stat WHERE url = '" . get_permalink($post_id) . "'"; 

    // Do API call 
    $response = wp_remote_retrieve_body(wp_remote_get('https://api.facebook.com/method/fql.query?format=json&query=' . urlencode($fql))); 

    // If error in API call, stop and don't store transient 
    if (is_wp_error($response)) 
     return 'error'; 

    // Decode JSON 
    **$json = json_decode($response);** 

    // Set total count 
    $count = absint($json[0]->total_count); 

    // Set transient to expire every 30 minutes 
    set_transient('ds_post_like_count' . $post_id, absint($count), 30 * MINUTE_IN_SECONDS); 

    } 

return absint($count); 

} /** Facebook End */ 

function ds_social_media_icons() { 



// Get the post ID 
    $post_id = get_the_ID(); ?> 
    <?php print_r($response) ?> 
    <div class="social-icons-wrap"> 
    <ul class="social-icons"> 
     <!-- Facebook Button--> 
     <li class="social-icon facebook"> 
      <a onclick="javascript:popupCenter('https://www.facebook.com/sharer/sharer.php?u=<?php the_permalink(); ?>&amp;appId=XXX_YOUR_FACEBOOK_APP_ID','Facebook Share', '540', '400');return false;" href="https://www.facebook.com/sharer/sharer.php?u=<?php the_permalink(); ?>&amp;appId=XXX_YOUR_FACEBOOK_APP_ID" target="blank"><i class="fa fa-facebook"></i> Share </a><span class="share-count"><?php echo ds_post_like_count($post_id); ?></span> 
     </li> 
     <!-- Twitter Button --> 
     <li class="social-icon twitter"> 
      <a onclick="javascript:popupCenter('https://twitter.com/share?&amp;url=<?php the_permalink(); ?>&amp;text=<?php the_title(); ?>&amp;via=XXX_YOUR_TWITTER_HANDLE','Tweet', '540', '400');return false;" href="https://twitter.com/share?&amp;url=<?php the_permalink(); ?>&amp;text=<?php the_title(); ?>&amp;via=XXX_YOUR_TWITTER_HANDLE" target="blank"><i class="fa fa-twitter"></i> Tweet </a><span class="share-count"><?php echo ds_post_tweet_count($post_id); ?></span> 
     </li>  
    </ul> 
    </div><!-- .social-icons-wrap --> 

<?php } 
/* DON'T DELETE THIS CLOSING TAG */ ?> 

要将输出用作数组,需要将第二个参数设置为true。

if(! is_wp_error($response) 
     && isset($response['response']['code'])   
     && 200 === $response['response']['code']) 
    { 
     $body = wp_remote_retrieve_body($response); 
     $fb = json_decode($body); 

     if(! isset($fb->likes) && isset($fb->shares)) 
     { 
      $fb->likes = $fb->shares; 
     } 

     if(isset($fb->likes)) 
     { 
      $myfblikes = sprintf('%04s', (int) $fb->likes); 
      update_post_meta($post->ID, 'fb_likes', $myfblikes); 
     } 
    } 

你可以参考this

+0

$ JSON = json_decode($响应,真正的);这最终不会让搜索结果呈现..看到,作为一个解决方案在几个网站,但不管这个为什么原因,虽然 –

+0

print_r($ response)是什么? –

+0

请原谅我的php无知,我将“print_r .....”添加到呈现结果的函数php的区域中。但错误是10行之前,所以它停在'致命错误:不能使用stdClass类型的对象作为数组在'..'哪里是添加打印的最佳位置。就像我说的抱歉,我没有做很多PHP –