Last active
August 18, 2017 22:55
-
-
Save CircleSquaredPublishing/d04926056b5e926d53c94cd6c166ec2a to your computer and use it in GitHub Desktop.
PHP function for retrieving total review count from a Facebook page.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function get_facebook_review_count() { | |
| // Build the URL that will call the Facebook API endpoint that contains the | |
| // total number of reviews for our page. | |
| $url = 'https://graph.facebook.com/v2.10/'; | |
| $url .= 'CircleSquaredPublishing'; | |
| $url .= '?fields=rating_count&'; | |
| // The app token is defined as a PHP constant in the wp-config.php file. This | |
| // is a best practice for security purposes. | |
| $url .= CIRCLE_SQUARED_APP_TOKEN; | |
| // Use PHP's built is CURL library to send a request to the URL created above. | |
| $curl = curl_init(); | |
| curl_setopt($curl, CURLOPT_URL, $url); | |
| curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | |
| // Store the results in the $response variable. | |
| $response = curl_exec($curl); | |
| // Decode the returned JSON data and store it in the $results variable. | |
| $results = json_decode( $response, true ); | |
| curl_close($curl); | |
| // Store the total number of reviews in the $review_count variable. | |
| $review_count = $results['rating_count']; | |
| // Store the URL to the reviews page on Facebook in the $reviews_page_url variable. | |
| $reviews_page_url = 'https://www.facebook.com/pg/CircleSquaredPublishing/reviews'; | |
| // Build the HTML output with Schema Markup and inject the $reviews_page_url | |
| // and $review_count variables at the appropriate locations. | |
| $output = '<p>'; | |
| $output .= '<span itemscope itemtype="http://schema.org/Product">'; | |
| $output .= '<span itemprop="name">Circle Squared Publishing is</span>'; | |
| $output .= '<span itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">'; | |
| $output .= '<a href="' . $reviews_page_url . '" rel="nofollow"> Rated'; | |
| $output .= '<span itemprop="ratingValue"> 5</span>'; | |
| $output .= '/ 5 </a>based on '; | |
| $output .= '<span itemprop="reviewCount">'; | |
| $output .= '<a href="' . $reviews_page_url . '" rel="nofollow">' . $review_count . '</a>'; | |
| $output .= '</span> reviews.'; | |
| $output .= '</span></span>'; | |
| $output .= '</p>'; | |
| // Return the output. | |
| return $output; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment