I am using Facebook ads api to manage Ads from my application and using fb PHP sdk 3.0 for this purpose.
I am trying to create image ads and using following code for the same.
$account_id = "<MY_ACOUNT_ID>";
$ad_account = new AdAccount($account_id);
$photo_data = new AdCreativePhotoData();
$photo_data->setData( array( AdCreativePhotoDataFields::URL => $data['url']) );
$object_story_spec = new AdCreativeObjectStorySpec();
$object_story_spec->setData(array(
AdCreativeObjectStorySpecFields::PAGE_ID => <MY_PAGE_ID>,
AdCreativeObjectStorySpecFields::PHOTO_DATA => $photo_data,
'link'=>$data['link']
));
$creative_params = array();
$creative = new AdCreative(null, $account_id);
$creative_params['name'] = 'MY TEST ADS FROM API';
$creative_params['link_url'] = '<LINK URL OF IMAGE/WEBSITE>';
$creative_params['object_story_spec'] = $object_story_spec;
$creative->setData( $creative_params );
try{
$creative->create();
$fields = array();
$params = array(
'name' => 'MY TEST IMAGE ADS : VISH',
'adset_id' => $data['adset_id'],
'creative' => array('creative_id' => $creative->id),
'status' => $data['status'],
);
try{
$ads = $ad_account->createAd($fields, $params);
return $ads->id;
} catch (Exception $e){
throw new Exception($e->getErrorUserTitle());
}
} catch (Exception $e){
throw new Exception($e->getErrorUserTitle());
}
This generate ads in Facebook but when i see its preview in ads manager panel , it show this error.
Can anybody tell me , how to fix this issue.
Related
I'm trying to create a ad via the Facebook Business SDK. Everything works well until I'm trying to create a AdCreativeVideoData. Code:
protected function createAdVideoCreative($thumbnail_url, $video_id, $name){
$video_data = new AdCreativeVideoData();
$video_data->setData(array(
AdCreativeVideoDataFields::IMAGE_URL => $thumbnail_url,
AdCreativeVideoDataFields::VIDEO_ID => $video_id,
AdCreativeVideoDataFields::CALL_TO_ACTION => array(
'type' => AdCreativeCallToActionTypeValues::LIKE_PAGE,
'value' => array(
'page' => FbAds::PAGE_ID,
),
),
));
$object_story_spec = new AdCreativeObjectStorySpec();
$object_story_spec->setData(array(
AdCreativeObjectStorySpecFields::PAGE_ID => FbAds::PAGE_ID,
AdCreativeObjectStorySpecFields::VIDEO_DATA => $video_data,
));
$creative = new AdCreative(null, FbAds::AD_ACCOUNT_ID);
$creative->setData(array(
AdCreativeFields::NAME => $name,
AdCreativeFields::OBJECT_STORY_SPEC => $object_story_spec,
));
try {
$creative->create();
return $creative;
} catch (Exception $e) {
print("Create Ad Video Creative Exception: " . $e->getMessage() . " (" . $e->getCode() . ")");
exit;
}
}
The above method is called when the selected video is uploaded to Facebook via the following method:
protected function createAdVideo($video_path){
$video = new Advideo(null, FbAds::AD_ACCOUNT_ID);
$video->{AdVideoFields::SOURCE} = $video_path;
try {
$video->create();
return $video->{AdVideoFields::ID};
} catch (Exception $e) {
print("Create Ad Video Exception: " . $e->getMessage() . " (" . $e->getCode() . ")");
exit;
}
}
The problem is that when I'm trying to create the AdCreativeVideoData, the following error is thrown:
[message] => Invalid parameter
[type] => OAuthException
[code] => 100
[error_subcode] => 1885252
[is_transient] =>
[error_user_title] => Video not ready for use in an ad
[error_user_msg] => The video is still being processed. Please wait for the video to finish processing before using it in an ad.
[fbtrace_id] => AwW0d9+Piz1
As you can see, the video is not yet processed. My question is: how can I check the status of the video? Is there a endpoint available somewhere which I can ping to check the status? The documentation states that I can check the status, but the AdVideo object in the createAdVideo() method doesn't have a status field:
I'm at a loss here so I hope someone can shed a light on this problem. Thanks in advance!
AdVideo does not have a status field since then, but Video does: https://developers.facebook.com/docs/graph-api/reference/video
Internally it's the same id, so you can request https://graph.facebook.com/v4.0/{video-id}?fields=id,status which will return the status of the uploaded (Ad)Video.
I'll assume it is because the video is not uploaded at all.
Instead of using "source" try using "file_url". You may also want to add a parameter "title" so it will not be named untitled video- but it is not required.
And try using the SDK smarter like so:
$myVideoUpload = (new AdAccount("act_123456676"))-
>createAdVideo(
array() //fields
array( //params
"file_url"=>"http://whatever.com",
"title"=>"my title"
)
);
if this works, it'll return a json_encodes string with id=video_id.
If you want to be able to retrieve errors- if any- and a general method for all api calls, do use the graph api as such:
$fb = new Facebook(array(
"app_id"=>"blalaa",
"app_secret"=>"blaaaaa",
"default_graph_version"=>"v9.0"
));
$url = "/act_123456789/advideos";
$access_token = "my token";
$params =
array("file_url"=>"https://whatever.com","title"=>"some video");
try{
$response = $fb->post(
$url,
$params,
$access_token
)
$response = $response->getGraphNode();
} catch(FacebookResponseException $e) {
return "graph error:: " . $e->getMessage();
} catch(FacebookSDKException$e) {
return "sdk error:: " . $e->getMessage();
}
The last one can be applied to everything given that you have an access_token that has access to the specific edge that you are requesting thus only he URL needs to be changed accordingly along with the parameters.
Note:
While using the graph-api: Use POST if you want to change or create something, and GET if you only want to read.
I have problem with Facebook SDK auto post to my Facebook business Page.
It is working code but now it's showing me error
"(#100) Only owners of the URL have the ability to specify the picture, name, thumbnail or description params. FacebookApiException"
I create cakePHP script to auto post Facebook to my Page. I create new App -> I took App Id and Secret ID. I created Access Token with long time expired. Of course I verifed domain for my Page with bussiness acount on FB.
What is wrong...? I don't have any idea!
please find my code
$config = array();
$config['appId'] = '16xxxxxxxxxxxx';
$config['secret'] = 'a24faxxxxxxxxxxxxxxxxxxxxx';
$config['fileUpload'] = false; // optional
$link = SITE_URL.'/PostAds/details/'.base64_encode($post_id);
$name = $this->request->data['title'];
$price = $this->request->data['price'];
$city = $this->request->data['city'];
$fb = new \Facebook($config);
$img_link = webroot.'/uploads/postAd/'.$image_name;
$params = array(
"access_token" => 'EAACZA18CF5TcBAL0E4XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
"message" => $name.' - '.$city.' Price:'.$price,
"link" => $link,
"picture" => $img_link,
"name" => $name,
"description" => $name.' '.$price,
"published" => true
);
try {
$ret = $fb->api('/page-id/feed', 'POST', $params);
echo 'Successfully posted to Facebook';
} catch(Exception $e) {
echo $e->getMessage();
}
I have been trying over and over, but could not reach any result.
the code is generating permission id and I don't know what that means.
Please do help if anyone succeeded in this before, I just want to share file publicly using the google drive api v2.0
$fileId = '18mWN0UWX_z-4A1gag85ou0Im-wvKfMZU-tibdVd8nxY';
$userPermission = new Google_Service_Drive_Permission(array(
'type' => 'anyone',
'role' => 'reader',
'emailAddress' => 'user#example.com'
));
$request = $service->permissions->create(
$fileId, $userPermission, array('fields' => 'id'));
$batch->add($request, 'user');
$domainPermission = new Google_Service_Drive_Permission(array(
'type' => 'domain',
'role' => 'reader',
'domain' => 'example.com'
));
$request = $service->permissions->create(
$fileId, $domainPermission, array('fields' => 'id'));
$batch->add($request, 'domain');
$results = $batch->execute();
foreach ($results as $result) {
if ($result instanceof Google_Service_Exception) {
// Handle error
printf($result);
} else {
printf("Permission ID: %s\n", $result->id);
}
}
} finally {
$service->getClient()->setUseBatch(false);
}
Here is my code snippet which was 2 years old.
$uplodedOriginalFile = new Google_Service_Drive_DriveFile();
$originallinkdata = file_get_contents($downloadlink['originallink']);
$uploadedfile = $service->files->insert($uplodedOriginalFile, array(
'data' => $originallinkdata,
'uploadType' => 'multipart',
));
$newPermission = new Google_Service_Drive_Permission();
//$newPermission->setValue($value);
$newPermission->setType('anyone');
$newPermission->setRole('reader');
try
{
$service->permissions->insert($uploadedfile['id'], $newPermission);
}
catch (Exception $e)
{
print "An error occurred: " . $e->getMessage();
}
$publicOriginallink = "https://googledrive.com/host/".$uploadedfile['id'];
So you just need the inserted file Id and keep the permssion for anyone as reader and append the inserted file Id after "https://googledrive.com/host/ [newly inserted file id which is returned by google drive sdk]"
code snippet worked
$fileid =$createdFile['id'];
//--insert permission to file in public
$newPermission = new Google_Permission();
$newPermission->setType('anyone');
$newPermission->setRole('reader');
try
{$service->permissions->insert($fileid, $newPermission);}
catch (Exception $e){print "An error occurred: " . $e->getMessage();}
$publicOriginallink = "https://googledrive.com/host/".$fileid;
I just made few changes with Jai's code (Google_Permission) to match with my Google APIs Client Library version.
I am trying to write my own auto poster for a Facebook Page or 2, and it will not upload photos to the page.
Code:
<?php
require_once("./src/facebook.php");
require("functions.php");
$f = getFB();
$config = array(
"appId" => $f['appID'],
"secret" => $f['secret'],
"fileUpload" => true,
"allowSignedRequest" => false
);
$fb = new Facebook($config);
$uid = $fb -> getUser();
$param = array(
"access_token" => $f['accessToken'],
"message" => $_GET['msg'],
"url" => "http://i.imgur.com/lHkOsiH.png",
);
try
{
$ret = $fb -> api('/me/feed', 'POST', $param);
echo "Successfully posted!";
print_r($ret);
}
catch (Exception $e)
{
die($e -> getMessage());
}
?>
It will upload just fine, but the result looks like this
How can I get the API to actually upload an image?
Simple error you used /me/feed which targets the user feed, to target the user's photos use me/photos instead, so the final API call would look like
$fb->api('/me/photos', 'POST', $param);
I need my application to publish a post on my Wall page automatically and for that use PHP-SDK, but the publication does not appear. Until the notification that the publication occurred, but it does not appear in the Wall page. In the same script, if I change the page_id by "me" the publication is made in the wall of my profile perfectly.
See code:
$fb_page_id = 'xxxxxxxxxx';
$fb_app_id = 'xxxxxxxxxxxxxxxxxxxx';
$fb_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$fb_app_url = 'https://apps.facebook.com/application/';
$fb_app_baseUrl ="http://www.mysite.com/app/application/";
$msg = array(
'message' => 'date: ' . date('Y-m-d') . ' time: ' . date('H:i')
);
//construct the message/post by posted data
$msg['message'] = 'Test';
$msg['link'] = $link;
$msg['picture'] = "http://www.mysite.com/app/facebook/icone_75_75.jpg";
$msg['name'] = $titulo;
$msg['caption'] = '';
$msg['description'] = $texto;
$msg['actions'] = array( array('name' => 'test', 'link' => 'http://www.mysite.com'));
//Create facebook application instance.
$facebook = new Facebook(array(
'appId' => $fb_app_id,
'secret' => $fb_secret
));
try {
$facebook->api('/'.$fb_page_id.'/feed', 'POST', $msg);
} catch (FacebookApiException $e) {
print_r($e);
}
thanks.
Now I created a database for storing received tokens. I used the token to post, but did not work!