I have a script that pulls the facebook likes via facebook graph and writes them in my DB. However it uses file_get_contents_curl for the request and this is causing problems, so I’d like to use the wp_remote_get() command. I’ve tried to change it but somehow I can’t do it (I don’t really have PHP knowledge).
Here’s the part of the script:
foreach($posts as $post)
{
$fb = json_decode(file_get_contents_curl('https://graph.facebook.com/?id='.get_permalink($post->ID)));
if( !isset( $fb->likes) && isset($fb->shares) )
{
$fb->likes = $fb->shares;
}
//$fb->likes = isset($fb->likes) ? $fb->likes : 0;
$myfblikes = sprintf("%04s", (int)$fb->likes);
update_post_meta($post->ID, 'fb_likes', $myfblikes);
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can try this:
foreach( $posts as $post )
{
$url = sprintf( 'http://graph.facebook.com/?id=%s', get_permalink( $post->ID ) );
$response = wp_remote_get( $url, array( 'timeout' => 15 ) );
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 );
}
}
}
where we can use wp_remote_retrieve_body() to get the response body of wp_remote_get().
Method 2
wp_remote_get returns an array, not a string. So,
foreach($posts as $post) {
// get remote document as JSON
$json = wp_remote_get( 'http://graph.facebook.com/?id=' . get_permalink($post->ID) );
// check for WP_Error
if(is_wp_error($json))
return false;
// Decode JSON and get document body as associative array
$fbData = json_decode($json['body'], true);
// return (numeric) array value
return intval($fbData['likes']);
}
I’m not sure about get_permalink($post->ID) part. That’s OK only if you have a FB page for each post.
Method 3
This solved my problem, try out this
$response = wp_remote_get( 'http://www.example.com/index.html' );
if( is_array($response) ) {
$header = $response['headers']; // array of http header lines
$body = $response['body']; // use the content
}
echo $body; // to print content
Method 4
Just change one function with the other. This requires also to change the way you are using json_decode() because wp_remote_get() returns an array and json_decode() requires a string.
foreach($posts as $post) {
$fb = wp_remote_get( 'http://graph.facebook.com/?id=' . get_permalink( $post->ID ) );
if( ! is_wp_error( $fb ) ) {
$body = json_decode( wp_remote_retrieve_body( $fb ) );
if( ! isset( $body->likes) && isset( $body->shares ) ) {
$body->likes = $body->shares;
}
$myfblikes = sprintf( "%04s", (int) $body->likes );
update_post_meta( $post->ID, 'fb_likes', $myfblikes );
}
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0