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('http://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);
}
You can try this:
where we can use
wp_remote_retrieve_body()
to get the response body ofwp_remote_get()
.wp_remote_get
returns an array, not a string. So,I’m not sure about
get_permalink($post->ID)
part. That’s OK only if you have a FB page for each post.This solved my problem, try out this
Just change one function with the other. This requires also to change the way you are using
json_decode()
becausewp_remote_get()
returns an array andjson_decode()
requires a string.