1

found this code online and it worked then all of a sudden one day i get "Fatal error: Cannot use object of type stdClass as array in.. functions.php" on this line $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 */ ?>

1 Answer 1

0

To use output as an array, you need to set the second parameter to 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 );
       }
    }

you can refer to this,

Sign up to request clarification or add additional context in comments.

8 Comments

$json = json_decode($response, true ); this ends up not letting the search results render.. is saw that as a solution on a few sites but doesn't work for this for whatever reason , thanks though
print_r($response) is what?
pardon my php ignorance, i added "print_r....." to the area of the functions php that renders the results. but the error is 10 lines before it so it stops at the 'Fatal error: Cannot use object of type stdClass as array in..' where is the best place to add the print. like i said sorry i don't do much php
from the error, the $response is not a json string. it may be a object, you can use the gettype() to see what the type is.
i need to research how to use gettype(),until then, could a php update or something on the server be interfering with json? or is it pretty clear it is in in the code from that error. I ask because it worked for months until today. I appreciate all your help.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.