Batch Requests and attached files with PHP SDK - php

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.

Related

Dropbox API - Error: expected list got dict

I am currently building a routine that needs to download files from one specific Dropbox folder , send them to another server and then move them to another folder on Dropbox.
I am using the /files/move_batch API endpoint for Dropbox to do so.
Here are the params sent to the API to move multiples files (well I'm only trying to move one file right now as it's still not working) :
$params = array(
'headers' => array(
'method' => 'POST',
'content-type' => 'application/json; charset=utf-8',
),
'body' => json_encode(array(
'entries' => array(
'from_path' => self::$files[0],
'to_path' => '/Applications/Archives/' . substr(self::$files[0], strrpos(self::$files[0], '/') + 1),
),
'autorename' => true,
)),
);
But I keep getting the same error message :
Error in call to API function "files/move_batch": request body: entries: expected list, got dict
I don't know what the API means by a list or how it should be formated.
The entries value should be a list of dict, one per file you want to move, each one containing both a from_path and a to_path. Your code is supplying the entries value to be a single dict though. (In PHP you can make both lists and dicts using the array keyword.)
It's easier to see and work with when you break it into pieces. Here's a working sample that does that.
<?php
$fileop1 = array(
'from_path' => "/test_39995261/a/1.txt",
'to_path' => "/test_39995261/b/1.txt"
);
$fileop2 = array(
'from_path' => "/test_39995261/a/2.txt",
'to_path' => "/test_39995261/b/2.txt"
);
$parameters = array(
'entries' => array($fileop1, $fileop2),
'autorename' => true,
);
$headers = array('Authorization: Bearer <ACCESS_TOKEN>',
'Content-Type: application/json');
$curlOptions = array(
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($parameters),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_VERBOSE => true
);
$ch = curl_init('https://api.dropboxapi.com/2/files/move_batch');
curl_setopt_array($ch, $curlOptions);
$response = curl_exec($ch);
echo $response;
curl_close($ch);
?>
To move just one file using this batch endpoint, you would change that line to something like:
'entries' => array($fileop1),

reCAPTCHA v.2 file_get_contents error

I made custom register page on wordpress using google recaptcha. It worked fine, without any errors but when i copied a code to new project it isnt working. No clue why. No secret and sitekey errors.
if(!empty($_POST['g-recaptcha-response']))
{
$gurl = 'https://www.google.com/recaptcha/api/siteverify';
$gdata = array(
'secret' => 'secret',
'response' => $_POST['g-recaptcha-response']
);
// use key 'http' even if you send the request to https://...
$goptions = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($gdata),
),
);
$gcontext = stream_context_create($goptions);
$gresult = file_get_contents($gurl, false, $gcontext);
var_dump($_POST['g-recaptcha-response']);
$gresult=json_decode($gresult);
if(!$gresult->success)
$register_errors[]='Error reCAPTCHA.';
}
UPDATE:
output of var_dump($_POST['g-recaptcha-response']) is some string propably hashed. $gresult = file_get_contents($gurl, false, $gcontext); is giving FALSE on var_dump($gresult);

Update video using youtube api

I want to update a video using google api v3 and i get the error 400 Bad Request.
This is my code.
$url = 'https://www.googleapis.com/youtube/v3/videos?part=snippet&videoId='.$_GET['videoId'].'&access_token='.Session::get('access_token');
$params = array(
"id"=> $_GET['videoId'],
"kind"=> "youtube#video",
'snippet' => array(
"title"=> "I'm being changed.",
"categoryId"=> "10",
"tags"=> array(
"humanities",
"Harpham",
"BYU"
),
'description' => 'test!'
)
);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'PUT',
'content' => http_build_query($params),
),
);
$context = stream_context_create($options);
$result = json_decode(file_get_contents($url, false, $context));
I think since you don't set all parameters inside snippet, that's giving an error. What you can do is, first getting that video with videos->list, then updating the field you are interested in and sending back the update request with the whole object back.
Here's an example also utilizing php client library: https://github.com/youtube/api-samples/blob/master/php/update_video.php

How to disable redirects when doing a URLFetch in PHP for Google App Engine?

I'm trying to set up two PHP on GAE sites: One to act as a web services layer, and another to act as the front-end. So far, things are great. I'm trying to secure communications between these two layers, so that not only I can call my web services.
I found this article that talks about making requests, and by doing so, it can set the X-Appengine-Inbound-Appid header, which is exactly what I want.
https://developers.google.com/appengine/docs/php/urlfetch/#PHP_Making_requests
To do this, the article says I should "consider telling the UrlFetch service to not follow redirects when invoking it."; then promptly does not provide any documentation on how you actually DO that.
My request looks like this:
$data = ['data' => 'this', 'data2' => 'that'];
$data = http_build_query($data);
$context = [
'http' => [
'method' => 'get',
'header' => "custom-header: custom-value\r\n" . "custom-header-two: custome-value-2\r\n",
'content' => $data,
]
];
$context = stream_context_create($context);
$result = file_get_contents('urlgoeshere', false, $context);
Thoughts & help are appreciated!
Two ways, either set follow_location to false and/or max_redirects to 0.
$context = [
'http' => [
'method' => 'get',
'header' => "custom-header: custom-value\r\n" . "custom-header-two: custome-value-2\r\n",
'content' => $data,
'follow_location' => false,
]
];
or
$context = [
'http' => [
'method' => 'get',
'header' => "custom-header: custom-value\r\n" . "custom-header-two: custome-value-2\r\n",
'content' => $data,
'max_redirects' => 0,
]
];

Fetching key details in php

This is the code from twilio api. Here 'callsid' is a query string.
$url = 'http://xxxx.com/phone/customer?to='.$number;
$call= $this->twilio->account->calls->get($this->request->query->get('CallSid'));
$call->update(array(
'Url' => $url,
'Method' => 'GET',
'StatusCallbackMethod' => 'GET',
'StatusCallback' => 'http://xxxx.com/phone/log/callback'
));
My question is that can we place an array key in the place of query string to fetch the key details? Like this:
$url = 'http://xxxx.com/phone/customer?to='.$number;
$call = $this->twilio->account->calls->get($this->request->query->get('url'));
$call->update(array(
'Url' => $url,
'Method' => 'GET',
'StatusCallbackMethod' => 'GET',
'StatusCallback' => 'http://xxxx.com/phone/log/callback'
));
Ricky from Twilio here.
If you want to filter by something other than CallSid with our PHP library you can use an iterator. For example this code will return calls that are currently in progress:
$filteredCalls = $client->account->calls->getIterator(
0, 50, array("Status" => "in-progress"));
foreach($filteredCalls as $call) {
print $call->price . '\n';
print $call->duration . '\n';
}
You can view a list of available filters in the docs.

Categories