I am currently posting images URLs on different facebook pages but I am using many calls to do the same thing.
Instead of this, I am trying to work on a batch request to save on execution time but I have an issue.
Doing many calls, I use this:
$urlLink = '/' . $pageId . '/photos';
$args = array(
'url' => $this->image_url,
'message' => $this->message,
'published' => false,
'scheduled_publish_time' => strtotime($this->programmed_dt),
);
$res = $this->fb->api($urlLink, 'POST', $args);
It works fine.
With the batch request I tried with that:
$urlLink = '/' . $facebookPage['id'] . '/photos';
$args['access_token'] = $facebookPage['access_token'];
$queries[] = array('method' => 'POST',
'relative_url' => $urlLink,
'body' => $args,
'url' => $this->image_url
);
$res = $this->fb->api('?batch=' . json_encode($queries), 'POST');
The response I have is:
{"error":{"message":"(#324) Requires upload file","type":"OAuthException","code":324}}
I tried to change the name field with all possibilities to send the image link, without success...
Any ideas for batch requests with image urls?
According to https://developers.facebook.com/docs/graph-api/making-multiple-requests/#multiple_methods I assume that your code which forms the request doesn't have the correct syntax.
From my point of view it should be the following:
$args['access_token'] = $facebookPage['access_token'];
$queries[] = array('method' => 'POST',
'relative_url' => $urlLink,
'body' => 'url=' . $this->image_url
);
$res = $this->fb->api('/?batch=' . json_encode($queries) . '&access_token=' . $args['access_token'], 'GET');
I can see everything ok in your code except this-
You should set upload support to true before posting image.
$this->fb->setFileUploadSupport(true);
for more detail you can check this answer:
CHECK HERE
Related
I have a php script to update my subscribers from my wordpress userlist to Mailchimp using batch-subscribe. (https://apidocs.mailchimp.com/api/2.0/lists/batch-subscribe.php)
Everything works fine when I submit about 400 records. All records are added, and I get a return from the API with the number of added records, etc.
If I submit about 600 or more (I have about 730 subscribers), all records are added to Mailchimp, but the API returns FALSE. I double checked it with === false, and it is false. I get no errors -- it just returns false (but all records are added to Mailchimp).
Mailchimp says "Maximum batch sizes vary based on the amount of data in each record, though you should cap them at 5k - 10k records, depending on your experience." (https://apidocs.mailchimp.com/api/2.0/lists/batch-subscribe.php).
I'm nowhere close to that, and every record is being added to the mailchimp list just fine. I just don't get the return from the API.
I've increased my timeout value to 5 minutes. I also switched to using different records, suspecting I might have had a record with something that was causing it to mess up, but it had the same behavior with different records.
I'm using the DrewM library to interface with Mailchimp API version 2.0. I double checked to make sure DrewM is using post for the request, and it does. (https://github.com/drewm/mailchimp-api/)
Any ideas what is causing this?
Here is the code:
function mailchimpdailyupdate () {
set_time_limit(300);
$api = get_mc_api();
$mcListId = get_mc_mailing_list();
$MailChimp = new \Drewm\MailChimp($api);
...
foreach ( $blogusers as $user ) {
$userinfo = get_userdata( $user->ID );
$location = ...//code to get location
$merge_vars = array(
'FNAME'=> $userinfo->first_name,
'LNAME'=> $userinfo->last_name,
'MMERGE3'=> $userinfo->user_login, //username
'MMERGE6'=> $location //location
);
$batch[] = array(
'email' => array('email' => $user->user_email),
'merge_vars' => $merge_vars
);
} //end foreach
//mailchimp call
$retval = $MailChimp->call('lists/batch-subscribe', array(
'id' => $mcListId, // your mailchimp list id here
'batch' => $batch,
'update_existing' => true
)
);
if ($retval === false) {
echo "Mailchimp API returned false";
}
echo 'Added: ' . $retval['add_count'] . "<br/>";
echo 'Updated: ' . $retval['update_count'] . "<br/>";
echo 'Errors: ' . $retval['error_count'] . "<br/>";
}
With help from Mailchimp support, I was able to locate and solve the problem.
The issue was actually in the DrewM wrapper.
The content-length section of the header was apparently not working correctly on long calls. I removed it, and everything began working fine.
Original section of DrewM code (not working):
$result = file_get_contents($url, null, stream_context_create(array(
'http' => array(
'protocol_version' => 1.1,
'user_agent' => 'PHP-MCAPI/2.0',
'method' => 'POST',
'header' => "Content-type: application/json\r\n".
"Connection: close\r\n" .
"Content-length: " . strlen($json_data) . "\r\n",
'content' => $json_data,
),
)));
Updated section of code (working):
$result = file_get_contents($url, null, stream_context_create(array(
'http' => array(
'protocol_version' => 1.1,
'user_agent' => 'PHP-MCAPI/2.0',
'method' => 'POST',
'header' => "Content-type: application/json\r\n".
"Connection: close\r\n",
'content' => $json_data,
),
)));
Hi there i tried to get all the Profile-Images of the Pages i managed.
I tried something like this:
// my loop:
$request[] = array(
'method' => 'POST',
'relative_url' => '/'.$account->id.'/?fields=picture.type(large)&access_token='.$account->access_token.''
);
and then:
$batchResponse = $this->facebook->api('?batch='.json_encode($request),'POST',array('access_token' => $this->facebook->getAccessToken()));
without success :/
i also tried to set the access_token of the page in the body tag:
$request[] = array(
'method' => 'POST',
'relative_url' => '/'.$account->id.'/?fields=picture.type(large)'
'body' => '&access_token='.$account->access_token.''
;
Your PHP script should have you login. If the login was successful, the SDK will manage the access_token for you.
Then issue this one request:
$result = $this->facebook->api('/me/accounts?fields=name,picture.type(large)', 'GET');
If you don't want to authenticate, instead you can pass an array of ids:
$page_ids = array( _PAGE_ID_1, _PAGE_ID_2, ...);
$result = $this->facebook->api('/?ids=' . implode(',',$page_ids) . '&fields=name,picture.type(large)', 'GET');
For these queries, you are getting data, so you should be using a GET request. Within the Facebook API, POST requests are only used when you are sending data to Facebook.
I'm trying to send batch notification from my app to several app users after runing the below code I get an error in the response:
"{"error":{"message":"(#100) Must specify a non-empty template `param","type":"OAuthException","code":100}}"`
Although the template param is set...
Appreciate any help on what am i doing wrong..
Here is the code I use:
$batched_request = array();
foreach ($users as $idx => $user) {
$request = array(
'method' => 'POST',
'relative_url' => '/' . $user['id'].'/notifications',
'access_token' => $app_access_token,
'template' => $template,
'href' => $href
);
$batched_request[] = json_encode($request);
}
$params = array('batch' => '[' . implode(',',$batched_request) . ']' );
try {
$response = $facebook->api('/','POST',$params);
} catch(FacebookApiException $e) {
error_log($e);
}
if you post via batch api, please have in mind, that you should enclose the template & href parameter as a http query string within the "body"-key.
for example:
$apiCalls[] = array(
"method" => "POST",
"relative_url" => $user['id'] . "/notifications",
"body" => http_build_query(array("href" => $href, "template" => $template, "ref" => "ref_key")),
"access_token" => $app_access_token
);
I'm trying to make a batch request to post an unique photo on differents page.
For that, I wish to use Batch Post Requests to optimize the proccess.
My Code :
$facebook = new Facebook(array('appId' => myappId, secret => mysecret, 'cookie' => true, 'fileUpload' => true, 'domain' => $_SERVER['SERVER_NAME']));
$request[0] = array(
'relative_url' => 'facebookPageId1/photos'
'method' => 'post'
'body' => 'access_token=page_access_token_1&message=my_message&attached_files=' . basename($picture));
$request[1] = array(
'relative_url' =>'facebookPageId2/photos'
'method' => 'post'
'body' => 'access_token=page_access_token_2&message=my_message&attached_files=' . basename($picture));
$file[basename($picture)] = '#' . realpath($picture);
$batch = json_encode(array_values(requests));
$params = array('batch' => $batch);
$params = array_merge($params, $file);
$facebook->api('/', 'POST', $params)
Now when I am running this code I got the following output for my two requests :
'{"error":{"message":"(#324) Requires upload file","type":"OAuthException","code":324}}'
So what's the problem ?
I set fileUpload at true on my Facebook Object and I tried to post a photo on the url "pageId/photos" with a classic request and it's worked perfectly. But witch a batch request, I have always the same error.
Thanks for your help.
EDIT : Ok I get my mistake, my requests was wrong :
$request[0] = array(
'relative_url' => 'facebookPageId1/photos',
'method' => 'post',
'body' => 'access_token=page_access_token_1&message=my_message',
'attached_files' => basename($picture)
);
$request[1] = array(
'relative_url' =>'facebookPageId2/photos',
'method' => 'post',
'body' => 'access_token=page_access_token_2&message=my_message',
'attached_files' => basename($picture)
);
But now I got the following error :
{"error":{"message":"File picturename.jpg has not been attached","type":"GraphBatchException"}}
This is what i do:
$files['access_token']=$access_token;
$request[0] = array(
'relative_url' => 'facebookPageId1/photos',
'method' => 'POST',
'body' => 'message=my_message',
'attached_files' => 'file_0'
);
$files['file_0']= basename($picture);
$batchresult = $facebook->api("/?batch=".urlencode(json_encode($request)), 'POST', $files);
Facebook Batch ask you to put the files in diferent arrays, also you can put the access token in there, and you need to put it once.
I am trying to upload a photo to a specific page, which is working, but now I would like to tag the current authenticated user in that photo I have just uploaded to the page.
Here is my code,
$result = $facebook->api('/PAGE_ID/photos', 'post', array(
'source' => '#pic.jpeg',
'message' => 'Ninja of the month!!!',
'access_token' => 'PAGE_TOKEN',
'tags' => array(array(
'tag_uid'=> CURRENT_USERS_UID,
'x' => 0,
'y' => 0
))
));
when I try that I get this error Fatal error: Uncaught OAuthException: (#322) Invalid photo tag subject thrown, I have made sure that the page allows users to be tagged, and that I have the required permissions, status_update,publish_stream,user_photos,offline_access,manage_pages.
Any idea why this could be happening and how I can fix it?
I think you might need to update the tags with the different access_token (user's access_token).
something like this might work
$facebook->setFileUploadSupport(true);
$args = array(
'access_token' => 'PAGE_TOKEN',
'message' => 'MESSAGE',
'image' => '#' . realpath($path_to_user),
);
$data = $facebook->api('/ALBUM_ID/photos', 'post', $args);
$token = $facebook->getAccessToken();
$argstag = array('to' => 'USER_TO_TAG');
$argstag['x'] = 40;
$argstag['y'] = 40;
$argstag['access_token'] = $token;
$datatag = $facebook->api('/' . $data['id'] . '/tags', 'post', $argstag);
I think this may be a bug, I think we may be having the same problem:
http://facebook.stackoverflow.com/questions/8425605/cant-tag-users-on-facebook-page-php-sdk-graph-api
This is a link to someone who has posted the issue as a bug to facebook. I don't know if anyone is working on this but please add your vote to getting it fixed and leave a comment. I'm sure that a LOT of people would like this functionality!
http://bugs.developers.facebook.net/show_bug.cgi?id=17947