I'm trying to upload images using Graph API Batch Request, but i'm unable to upload using inline image, can anyone help me please?
Batch Request Docs: https://developers.facebook.com/docs/reference/api/batch/
Photo batch uploads: http://developers.facebook.com/blog/post/493/
Photo batch uploads blog post code works fine, but i want to upload images from my server and not from my pc, Ex: /public_html/image/pic.jpg and i'm not sure how i can do it.
EDIT: I'm using PHP SDK 3.0.1
Here's my code:
<?php
CODE WAS CHANGED AND THE PROBLEM IS FIXED ALREADY, SEE THE ANSWER BELOW
?>
This is from their docs:
Uploading binary data
Binary data can be specified as part of the multipart/mime portion of
the batch API request. The batch Graph API allows uploading multiple
binary items as part of a batch call. In order to do this, you need to
add all the binary items as multipart/mime attachments to your
request, and need each operation to reference its binary items using
the "attached_files" property in the operation. The "attached_files"
property can take a comma separated list of attachment names in its
value.
The following example shows how to upload 2 photos in a single batch
call:
curl
–F ‘access_token=…’ \
-F ‘batch=[{“method”:”POST”, \
“relative_url”:”me/photos”, \
“body”:”message=My cat photo” \
"attached_files":"file1" \
},
{“method”:”POST”, \
“relative_url”:”me/photos”, \
“body”:”message=My dog photo” \
"attached_files":"file2" \
},
]’
-F ‘file1=#cat.gif’ \
-F 'file2=#dog.jpg' \
https://graph.facebook.com
EDIT 2:
The first issue I see is that the Batch should not be part of the URL, but rather part of the params ...
See the crude batch example below:
$batch = array();
$req = array(
'method' => 'GET',
'relative_url' => '/me'
);
$batch[] = json_encode($req);
$req = array(
'method' => 'GET',
'relative_url' => '/me/albums'
);
$batch[] = json_encode($req);
$params = array(
'batch' => '[' . implode(',',$batch) . ']'
);
try {
$info = $facebook->api('/','POST',$params);
} catch(FacebookApiException $e) {
error_log($e);
$info = null;
}
if(!empty($info)){
if($info[0]['code'] == '200'){
$user_profile = json_decode($info[0]['body']);
}
if($info[1]['code'] == '200'){
$user_albums = json_decode($info[1]['body']);
}
echo "<pre>User Profile:\n";
print_r($user_profile);
echo "\nAlbums\n";
print_r($user_albums);
echo "<pre>";
}
Notice specifically how the $facebook->api call is formatted ...
EDIT:
Here is a working batch picture upload:
$files = array(
'/data/Pictures/pic1.jpg',
'/data/Pictures/pic2.jpg',
'/data/Pictures/pic3.jpg'
);
//Must set upload support to true
$facebook->setFileUploadSupport(true);
$batch = array();
$params = array();
$count = 1;
foreach($files as $file){
$req = array(
'method' => 'POST',
'relative_url' => '/me/photos',
'attached_files' => 'file' . $count
);
//add this request to the batch ...
$batch[] = json_encode($req);
//set the filepath for the file to be uploaded
$params['file'.$count] = '#' . realpath($file);
$count++;
}
$params['batch'] = '[' . implode(',',$batch) . ']';
try{
$upload = $facebook->api('/','post',$params);
} catch(FacebookApiException $e) {
error_log($e);
$upload = null;
}
//View the results ...
if(!is_null($upload)){
echo "<pre>" . print_r($upload,1) . "<pre>";
echo "<hr />";
}
Just tested and it works like a charm ...
Well, I'm not too sure and I cannot check at the moment, but
http://au.php.net/manual/en/function.curl-setopt.php
Look it up at CURLOPT_POSTFIELDS, it says:
The full data to post in a HTTP "POST" operation. To post a file,
prepend a filename with # and use the full path. The filetype can be
explicitly specified by following the filename with the type in the
format ';type=mimetype'. This parameter can either be passed as a
urlencoded string like 'para1=val1¶2=val2&...' or as an array with
the field name as key and field data as value. If value is an array,
the Content-Type header will be set to multipart/form-data. As of PHP
5.2.0, files thats passed to this option with the # prefix must be in array form to work.
Here is another CURL example:
CURL PHP send image
So what you need to do is
$queries = array(
array("method" => "POST", "relative_url" => "me/photos","body" => "message=cool","attached_files" => 'file1')
);
and
$batch = $facebook->api("/?batch=".json_encode($queries)."&file1=#pic.jpg", 'POST');
// // File you want to upload/post
$post_data['file1'] = "#D:/home/2.jpg";
$post_data['file2'] = "#D:/home/1.jpg";
// Initialize cURL
$ch = curl_init();
// Set URL on which you want to post the Form and/or data
curl_setopt($ch, CURLOPT_URL, $post_url);
// Data+Files to be posted
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
// Pass TRUE or 1 if you want to wait for and catch the response against the request made
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// For Debug mode; shows up any error encountered during the operation
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// Execute the request
$response = curl_exec($ch);
// echo curl_errno($ch);
// echo curl_error($ch);
// Just for debug: to see response
echo $response;
This will work for sure its working for me
Related
I am trying to use a PHP CURL request to upload data to Pass Slot to change an image, and I am continually getting errors.
This is the CURL request needed from their developer section on their website
POST https://api.passslot.com/v1/passes/pass.example.id1/27f145d2-5713-4a8d-af64-b269f95ade3b/images/thumbnail/normal
and this is the data that needs to be sent in its requested format
------------------------------330184f75e21
Content-Disposition: form-data; name="image"; filename="icon.png"
Content-Type: application/octet-stream
.PNG
imagedata
This is the code I am using currently, as I am not familiar with what is required on Multipart Form requests on API
$passId = "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx";
$pass_generate_url = "pass.xxxxxxxxxxxx";
$url1 = 'https://api.passslot.com/v1/passes/'.$pass_generate_url.'/'.$passId.'/images/strip/normal';
$logo_file_location = "image.png";
$logo_file_location1 = "http://xxxxxxx.com/uploads/";
$data1 = array('image' => '#uploads/'.$logo_file_location,'application/octet-string',$logo_file_location1,'some_other_field' => 'abc',);
$auth1 = array( 'Authorization: Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=',
'Content-Type: text/plain');
$ch1 = curl_init($url1);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1, CURLOPT_POST, 1);
curl_setopt($ch1, CURLOPT_POSTFIELDS, $data1);
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch1, CURLOPT_HTTPHEADER, $auth1);
$response1 = curl_exec($ch1);
$ch1 = curl_init($url1);
When I run the code, this is the response from the CURL I get
{"message":"Validation Failed","errors":[{"field":"image","reasons":["Required"]}]}
Is there something I need to add to make the code work please?
Yes, I think you can build your own curl including to use say PHP CURLFile (sending the Multipart delimiter boundary and then the graphic data, etc) But you may choose to use an API (Say PassSlot PHP SDK)
https://github.com/passslot/passslot-php-sdk
General usage
require 'passslot-php-sdk/src/PassSlot.php';
$engine = PassSlot::start('<YOUR APP KEY>');
$pass = $engine->createPassFromTemplate(<Template ID>);
$engine->redirectToPass($pass);
For PNG file, it is like:
<?php
require_once('../src/PassSlot.php');
$appKey ='<YOUR APP KEY>';
$passTemplateId = 123456;
$outputPass = FALSE;
$values = array(
'Name' => 'John',
'Level' => 'Platinum',
'Balance' => 20.50
);
$images = array(
'thumbnail' => dirname(__FILE__) . '/thumbnail.png'
);
try {
$engine = PassSlot::start($appKey);
$pass = $engine->createPassFromTemplate($passTemplateId, $values, $images);
if($outputPass) {
$passData = $engine->downloadPass($pass);
$engine->outputPass($passData);
} else {
$engine->redirectToPass($pass);
}
} catch (PassSlotApiException $e) {
echo "Something went wrong:\n";
echo $e;
}
For further reference, please visit this
https://github.com/passslot/passslot-php-sdk/blob/master/examples/example.php
You may also view the source of the API to get inspired:
https://github.com/passslot/passslot-php-sdk/blob/master/src/PassSlot.php
Additional remark:
In case there is certificate expiry warning/error when running the SDK, please download the latest cacert.pem from https://curl.se/docs/caextract.html and replace the one in the SDK
Hi guys I'm getting trouble uploading attachment in facebook messenger API
https://developers.facebook.com/docs/messenger-platform/reference/attachment-upload-api/
I'm using php code but I tried all possible action to upload but my code didn't work every time I send a post request I received this error
"message":"(#100) Incorrect number of files uploaded. Must upload
exactly one file.","type":"OAuthException","code":100"
I tried this curl pattern from the documentation
curl \
-F 'message={"attachment":{"type":"image", "payload":{"is_reusable":true}}}' \
-F 'filedata=#/tmp/shirt.png;type=image/png' \
"https://graph.facebook.com/v12.0/me/message_attachments?access_token=<PAGE_ACCESS_TOKEN>"
and here's my code PHP code
function uploadFromFile($psid,$file) {
$reply_message['message']['attachment'] = array(
'type' => 'image',
'payload' => array(
'is_reusable' => true
)
);
$reply_message['filedata'] = "#".$file['tmp_name].";type=image/png";
$url = "https://graph.facebook.com/v12.0/me/message_attachments?access_token=" . $this->accessToken;
$ch = curl_init();
$headers = array("Content-type: application/json");
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($reply_message));
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,$false);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$st = curl_exec($ch);
curl_close($ch);
file_put_contents("errormessage-api.txt",$st);
$result = json_decode($st,TRUE);
$this->send($psid,$result['attachment_id']);
return $result['attachment_id'];
}
I know the error is on my payload I don't know the right syntax. Thank you guys!
I have uploaded file to google drive with curl API. But I want to upload file to specific folder. I have also tried with folder id in API url like: https://www.googleapis.com/upload/drive/v3/files/folder_id?uploadType=media
add_action('wpcf7_before_send_mail', 'save_application_form');
function save_application_form($WPCF7_ContactForm) {
$wpcf7 = WPCF7_ContactForm :: get_current();
$submission = WPCF7_Submission :: get_instance();
if ($WPCF7_ContactForm->id == 8578) {
if ($submission) {
$submited = array();
//$submited['title'] = $wpcf7->title();
$submited['posted_data'] = $submission->get_posted_data();
$uploaded_files = $submission->uploaded_files();
$name = $submited['posted_data']["Name"];
$position_test = $submited['posted_data']["Position"];
$email = $submited['posted_data']["Email"];
$phone = $submited['posted_data']["phone"];
$message = $submited['posted_data']["Message"];
$position = $submited['posted_data']["AttachYourCV"];
// $test2 = $_FILES['AttachYourCV']['name'];
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$cf7_file_field_name = 'AttachYourCV';
$image_location = $uploaded_files[$cf7_file_field_name];
$mime_type = finfo_file($finfo, $image_location);
$token = GetRefreshedAccessToken('client_id', 'refresh_token', 'client_secret');
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'https://www.googleapis.com/upload/drive/v3/files?uploadType=media',
CURLOPT_HTTPHEADER => array(
'Content-Type:' . $mime_type, // todo: runtime detection?
'Authorization: Bearer ' . $token
),
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => file_get_contents($image_location),
CURLOPT_RETURNTRANSFER => 1
));
$response = curl_exec($ch);
$id = json_decode($response, TRUE);
$get_id = $id['id'];
curl_close($ch);
if (isset($id['id'])) {
$get_id = $id['id'];
$post_fields = array();
$folder_id = '1-9r8oVsAfV_iJmYh1cZYPMvE9Qhv8RLA';
// remove extension
$this_file_name = explode('.', $position);
// submit name field
$post_fields['name'] .= $this_file_name[0];
$post_fields['parents'] = $folder_id[1];
$ch2 = curl_init();
curl_setopt_array($ch2, array(
CURLOPT_URL => 'https://www.googleapis.com/drive/v3/files/' . $get_id,
CURLOPT_HTTPHEADER => array(
'Content-Type:application/json', // todo: runtime detection?
'Authorization: Bearer ' . $token
),
CURLOPT_POST => 1,
CURLOPT_CUSTOMREQUEST => 'PATCH',
CURLOPT_POSTFIELDS => json_encode($post_fields),
CURLOPT_RETURNTRANSFER => 1
));
$response = curl_exec($ch2);
if ($response === false) {
$output = 'ERROR: ' . curl_error($ch2);
} else {
$output = $response;
}
// close second request handler
curl_close($ch2);
}
}
I have added another curl call but still didn't get file in folder. My file untitled issue solved with this way.
Uploading files to google drive is a two part thing. (note it can be done as a single call but if you want to add the corect data its best to do it as two)
create Adds the initial meta data for the file. Name, media type, and location that being its parent directory.
Second the actual upload of the file itself.
Note: a file uploaded without first sending the metadata will create a dummy metadata most often with a title of the file being "unknown."
Create
When you do your create of the inertial metadata. You need to add the parents in this inital call the field is called parents[] you need to add the file id there. I cant help you do this as your not adding the code for it.
Upload
By default the file is uploaded into the root folder unless you add a parent folder id for the file.
If you check the documentation you will find the optional query parms
Try using &addParents=[folderId]
Example:
https://www.googleapis.com/upload/drive/v3/files?uploadType=media&addParents=1bpHmln41UI-CRe5idKvxrkIpGKh57T32
If you originally created the metadata for the file in the root directory and then try to upload the file to a different directory i suspect that the upload will create new metadata. These two calls need to be done the same. Your create should have parents set.
post body
$data = array("name" => "test.jpg", "parents" => array("123456"));
$data_string = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
I'm using file_get_contents() to get a PHP file which I use as a template to create a PDF.
I need to pass some POST values to it, in order to fill the template and get the produced HTML back into a PHP variable. Then use it with mPDF.
This works perfectly on MY server (a VPS using PHP 5.6.24)...
Now, at the point where I'm installing the fully tested script on the client's live site (PHP 5.6.29),
I get this error:
PHP Warning: file_get_contents(http://www.example.com/wp-content/calculator/pdf_page1.php): failed to open stream: HTTP request failed! HTTP/1.1 406 Not Acceptable
So I guess this can be fixed in php.ini or some config file.
I can ask (I WANT TO!!) my client to contact his host to fix it...
But since I know that hosters are generally not inclined to change server configs...
I would like to know exactly what to change in which file to allow the code below to work.
For my personnal knowledge... Obviously.
But also to make it look "easy" for the hoster (and my client!!) to change it efficiently. ;)
I'm pretty sure this is just one PHP config param with a strange name...
<?php
$baseAddr = "http://www.example.com/wp-content/calculator/";
// ====================================================
// CLEAR OLD PDFs
$now = date("U");
$delayToKeepPDFs = 60*60*2; // 2 hours in seconds.
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if(substr($entry,-4)==".pdf"){
$fileTime = filemtime($entry); // Returns unix timestamp;
if($fileTime+$delayToKeepPDFs<$now){
unlink($entry); // Delete file
}
}
}
closedir($handle);
}
// ====================================================
// Random file number
$random = rand(100, 999);
$page1 = $_POST['page1']; // Here are the values, sent via ajax, to fill the template.
$page2 = $_POST['page2'];
// Instantiate mpdf
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new mPDF( __DIR__ . '/vendor/mpdf/mpdf/tmp');
// GET PDF templates from external PHP
// ==============================================================
// REF: http://stackoverflow.com/a/2445332/2159528
// ==============================================================
$postdata = http_build_query(
array(
"page1" => $page1,
"page2" => $page2
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
// ==============================================================
$STYLE .= file_get_contents("smolov.css", false, $context);
$PAGE_1 .= file_get_contents($baseAddr . "pdf_page1.php", false, $context);
$PAGE_2 .= file_get_contents($baseAddr . "pdf_page2.php", false, $context);
$mpdf->AddPage('P');
// Write style.
$mpdf->WriteHTML($STYLE,1);
// Write page 1.
$mpdf->WriteHTML($PAGE_1,2);
$mpdf->AddPage('P');
// Write page 1.
$mpdf->WriteHTML($PAGE_2,2);
// Create the pdf on server
$file = "training-" . $random . ".pdf";
$mpdf->Output(__DIR__ . "/" . $file,"F");
// Send filename to ajax success.
echo $file;
?>
Just to avoid the "What have you tried so far?" comments:
I searched those keywords in many combinaisons, but didn't found the setting that would need to be changed:
php
php.ini
request
header
content-type
application
HTTP
file_get_contents
HTTP/1.1 406 Not Acceptable
Maaaaany thanks to #Rasclatt for the priceless help! Here is a working cURL code, as an alternative to file_get_contents() (I do not quite understand it yet... But proven functional!):
function curl_get_contents($url, $fields, $fields_url_enc){
# Start curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
# Required to get data back
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
# Notes that request is sending a POST
curl_setopt($ch,CURLOPT_POST, count($fields));
# Send the post data
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_url_enc);
# Send a fake user agent to simulate a browser hit
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11) AppleWebKit/601.1.56 (KHTML, like Gecko) Version/9.0 Safari/601.1.56');
# Set the endpoint
curl_setopt($ch, CURLOPT_URL, $url);
# Execute the call and get the data back from the hit
$data = curl_exec($ch);
# Close the connection
curl_close($ch);
# Send back data
return $data;
}
# Store post data
$fields = array(
'page1' => $_POST['page1'],
'page2' => $_POST['page2']
);
# Create query string as noted in the curl manual
$fields_url_enc = http_build_query($fields);
# Request to page 1, sending post data
$PAGE_1 .= curl_get_contents($baseAddr . "pdf_page1.php", $fields, $fields_url_enc);
# Request to page 2, sending post data
$PAGE_2 .= curl_get_contents($baseAddr . "pdf_page2.php", $fields, $fields_url_enc);
I'm trying to upload www hosted (e.g. http://www.google.se/intl/en_com/images/srpr/logo1w.png) files to a facebook album.
Creating an album works just fine, but I don't seem to uploading any photos. I'm using the facebook php-sdk ( http://github.com/facebook/php-sdk/ ) and the examples I already tried are:
Upload Photo To Album with Facebook's Graph API
How can I upload photos to album using Facebook Graph API
I'm guessing CURL uploads perhaps only can manage locally stored files and not web hosted ones.
Here's my code:
/*
Try 1:
$data = array();
$data['message'] = $attachment->post_title;
$data['file'] = $attachment->guid;
try {
$photo = $facebook->api('/' . $album['id'] . '/photos?access_token=' . $session['access_token'], 'post', $data);
} catch (FacebookApiException $e) {
error_log($e);
}
*/
// Try 2:
//upload photo
$file = $attachment->guid;
$args = array(
'message' => 'Photo from application',
);
$args[basename($file)] = '#' . realpath(file_get_contents($file));
$ch = curl_init();
$url = 'https://graph.facebook.com/' . $album['id'] . '/photos?access_token=' . $session['access_token'];
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);
//returns the photo id
print_r(json_decode($data,true));
...where attachment->guid contains the photo url.
I'm pretty much stuck right now...
i think the problem is here:
$args[basename($file)] = '#' . realpath(file_get_contents($file));
since you want to post a picture from another source (right?), you should save it temporarily on your own host.
i also needed to do something like this, and since i had to process the image, i used the following way:
$im = #imagecreatefrompng('http://www.google.se/intl/en_com/images/srpr/logo1w.png');
imagejpeg($im, 'imgs/temp/temp.jpg', 85);
$args['image'] = '#' . realpath('imgs/temp/temp.jpg');
the rest looks fine though
I'll suggest to use the Facebook php SDK, it will be easier and the code will work with future updates of the APIs:
Using the Graph API php sdk:
$fbk = new Facebook(/* conf */);
$fbk->setFileUploadSupport(true);
//If you are executing this in a script, and not in a web page with the user logged in:
$fbk->setAccessToken(/* access token from other sources */);
//To add to an album:
$fbk->api("/$albumId/photos", "POST",
array('source' => '#'. realpath($myPhoto), 'message' => "Nice photo"));
//To upload a photo directly (the album will be created automatically):
$fbk->api("/me/photos", "POST",
array('source' => '#'. realpath($myPhoto), 'message' => "Nice photo"));
Using cURL directly:
If your really want to use cURL, your code is almost correct, but the error is in the $args array:
$args = array(
'message' => 'Photo from application',
'source' => file_get_contents($file)
);
Since the key for the photo data is source, see the Facebook Doc
Note on the # in cURL:
Also notice that the # in cUrl means that the parameter will be replaced with the actual bytes of the file that follows the #, so it isn't required if you already put in the source parameter the actual bytes.
I'm guessing CURL uploads perhaps only can manage locally stored files and not web hosted ones.
No, that’s not the case.
But you need to give the full, publicly reachable HTTP URL to the image, without an # in front – and you have to use the parameter name url for this value.
https://developers.facebook.com/docs/reference/api/photo/:
You can also publish a photo by providing a url param with the photo's URL.