致命错误:无法将stdClass类型的对象用作..中的数组。。functions.php

致命错误:无法将stdClass类型的对象用作..中的数组。。functions.php,php,wordpress,Php,Wordpress,在网上找到这段代码,它工作了,然后突然有一天我遇到了致命的错误:无法在中使用stdClass类型的对象作为数组。。这一行的functions.php$count=absint$json[0]->total_count function ds_post_like_count( $post_id ) { // Check for transient if ( ! ( $count = get_transient( 'ds_post_like_count' . $post_id ) ) )

在网上找到这段代码,它工作了,然后突然有一天我遇到了致命的错误:无法在中使用stdClass类型的对象作为数组。。这一行的functions.php$count=absint$json[0]->total_count

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 );
       }
    }

您可以参考,

要将输出用作数组,需要将第二个参数设置为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 );
       }
    }

您可以参考,

$json=json\u decode$response,true;这将导致无法呈现搜索结果。。在一些网站上被视为一种解决方案,但无论出于何种原因,都无法实现这一点,感谢thoughprint\u r$响应是什么?请原谅我对php的无知,我添加了print\u r。。。。。指向呈现结果的php函数区域。但错误在它前面10行,因此它在“致命错误:无法将stdClass类型的对象用作..中的数组”处停止,此处是添加打印的最佳位置。正如我所说的抱歉,我没有对错误进行太多PHP操作,$response不是json字符串。它可能是一个对象,您可以使用gettype来查看它的类型。我需要研究如何使用gettype,在那之前,服务器上的php更新或其他东西会干扰json吗?或者很明显,这是在代码中的错误。我这样问是因为它一直工作了好几个月,直到今天。感谢您的帮助。$json=json\u decode$response,true;这将导致无法呈现搜索结果。。在一些网站上被视为一种解决方案,但无论出于何种原因,都无法实现这一点,感谢thoughprint\u r$响应是什么?请原谅我对php的无知,我添加了print\u r。。。。。指向呈现结果的php函数区域。但错误在它前面10行,因此它在“致命错误:无法将stdClass类型的对象用作..中的数组”处停止,此处是添加打印的最佳位置。正如我所说的抱歉,我没有对错误进行太多PHP操作,$response不是json字符串。它可能是一个对象,您可以使用gettype来查看它的类型。我需要研究如何使用gettype,在那之前,服务器上的php更新或其他东西会干扰json吗?或者很明显,这是在代码中的错误。我这样问是因为它一直工作了好几个月,直到今天。我感谢你的帮助。