mardi 3 mars 2015

Overwriting shared image on Facebook to Yoast image


I am facing issues with facebook images only sharing sometimes with the API. I have done some research and it seems it has to do with a bug by Facebook and https images.


http://ift.tt/1xDWXzP


This function is pulling an https image which is not working sometimes. I can put a http photo in yoast plugin for facebook image. However that image is not overwriting the https image. Here is the function I am working with. I am stuck trying to figure out how to pull the yoast image.



/*Publishes on facebook*/
function publishFacebook($campaignId){
global $wpdb;
session_unset();
$supportTableName = $wpdb->prefix . "user_campaign_support";
$config = social_createoptions(); //get session config for the auth we got from wordpress
require_once('include/Hybrid/Auth.php');
$post_object = get_post($campaignId); // retrieves post object
$campaign['message'] = $post_object->post_content; // saves post_content
$postmeta = get_post_meta( $campaignId);
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($campaignId) );
if(empty($thumb)){
$thumb['0'] = get_template_directory_uri().'/img/defaults/default_campaign.jpg';
}
$campaign['link'] = $postmeta['_campaign_link'][0];
$campaign['picture'] = $thumb['0'];
$campaign['name'] = $post_object->post_title;
$campaign['caption'] = get_post_meta( $campaignId, "_campaign_caption", true );
if ($campaign['link'] == "http://") {
$campaign['link'] = get_site_url();
}
$hold = getFacebookTokens($campaignId);
if ((count ($hold) > 0 ) AND (strlen($campaign['message']) > 1) AND (is_array($hold))) {
//If we have tokens here, proceed
foreach ($hold as $token) {
try {
$hybridauth = new Hybrid_Auth( $config );
if ($token['tries'] > 2) {
$hybridauth->storage()->set("hauth_session.facebook.is_logged_in", 1 );
$hybridauth->storage()->set("hauth_session.facebook.token.access_token", $token['token']);
$service = $hybridauth->getAdapter("Facebook");
} else {
$hybridauth->restoreSessionData($token['session']);
$service = $hybridauth->getAdapter("Facebook");
}
if (strlen($token['custom']) > 3) {
$message = $token['custom'];
} else {
$message = $campaign['message'];
}
if (strpos($message, "_-*-_") !== false) {
//friends tagged
$newmessage = explode ("_-*-_", $message);
$message = $newmessage[0];
$tags = $newmessage[1];
if (substr($tags, -1 ) == ",") {
$tags = substr($tags, 0, -1);
}
}
if (strlen($tags) > 5) {
$service->api()->api("me/feed", "post", array(
"message" => $message,
"picture" => $campaign['picture'],
"link" => $campaign['link'],
"name" => $campaign['name'],
"tags" => $tags,
"place" => 155021662189,
"caption" => $campaign['caption']
));
} else {
$service->api()->api("me/feed", "post", array(
"message" => $message,
"picture" => $campaign['picture'],
"link" => $campaign['link'],
"name" => $campaign['name'],
"caption" => $campaign['caption']
));
}
session_destroy();
//mark as published
$wpdb->update($supportTableName, array('published_facebook' => 1), array('campaign_id' => $campaignId, 'facebook_id'=> $token['id']));
} catch (Exception $e) {
//var_dump($e->getMessage());
$wpdb->query("UPDATE ".$supportTableName." SET tried_to_publish = tried_to_publish + 1 WHERE campaign_id=".$campaignId." AND facebook_id=".$token['id']);
//return true; //as in we had an error
}
}
return true; //return true if we decide to limit number of tokens one day
} else {
return false; //as in nothing left
}
}




Aucun commentaire:

Enregistrer un commentaire