How can i upload and post an image to linkedin using api? - php

I'm trying to create an app for posting images to linkedin. Linkedin has an api which accepts an image url and a message, but it seems to be not the exact way to post an image since it is showing my image as a link instead of an image. But in the case of Buffer App, it displays full image, not the URL. Is there any way to post images to linkedin via api. Anyone please help me to sort this out.

Use Linkedin V2 API.Below code will upload the image.
curl -i --upload-file /Users/peter/Desktop/superneatimage.png --header "Authorization: Bearer redacted" 'https://api.linkedin.com/mediaUpload/C5522AQGTYER3k3ByHQ/feedshare-uploadedImage/0?ca=vector_feedshare&cn=uploads&m=AQJbrN86Zm265gAAAWemyz2pxPSgONtBiZdchrgG872QltnfYjnMdb2j3A&app=1953784&sync=0&v=beta&ut=2H-IhpbfXrRow1'
I suggest you to use Guzzle HTTP client to exicute this in your PHP application

This works for me
$upUrl = "Your Url";
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, $upUrl);
curl_setopt($cURL, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array('Content-Type: application/binary'));
$path = "./uploads/image.jpg";
$file = fopen($path, 'r');
$size = filesize($path);
$fildata = fread($file, $size);
curl_setopt($cURL, CURLOPT_POSTFIELDS, $fildata);
curl_setopt($cURL, CURLOPT_INFILE, $file);
curl_setopt($cURL, CURLOPT_INFILESIZE, $size);
$response = curl_exec($cURL);
curl_close($cURL);

Related

How to stop PHP cURL upload inserting "Boundary" into the "Content-Type" field?

I'm using the below code to upload an MP4 file to a web service, using PHP cURL.
I've specified the 'Content-Type' as 'video/mp4', in CURLOPT_HTTPHEADER.
Unfortunately, having uploaded the file, the 'Content-Type' stored for it in the service displays as: "content_type":"video/mp4; boundary=----WebKitFormBoundaryfjNZ5VkJS8z3CB9X"
As you can see, the 'boundary' has been inserted into the 'content_type'.
When I then download the file, it fails to play, with a 'file unsupported/file extension incorrect/file corrupt' message.
$authorization = "Authorization: Bearer [token]";
$args['file'] = curl_file_create('C:\example\example.mp4','video/mp4','example');
$url='[example web service URL]';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data', 'Accept: application/vnd.mendeley-content-ticket.1+json', $authorization));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS , $args);
$response = curl_exec($ch); // URL encoded output - needs to be URL encoded to get the HREF link header
curl_close($ch);
Would be extremely grateful for any help, advice or pointers!
Maybe the API doesn't expects a POST multipart, but the actual contents in the body itself:
Ref: How to POST a large amount of data within PHP curl without memory overhead?
You need to use PUT method for the actual contents of the file to go inside the body - if you use POST, it will try to send as a form.
$authorization = "Authorization: Bearer [token]";
$file = 'C:\example\example.mp4';
$infile = fopen($file, 'r');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.mendeley.com/file_contents");
curl_setopt($ch, CURLOPT_PUT, 1 ); // needed for file upload
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file));
curl_setopt($ch, CURLOPT_INFILE, $infile);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST' );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: video/mp4', 'Accept: application/vnd.mendeley-content-ticket.1+json', $authorization));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result=curl_exec ($ch);
I have the same problem calling a Openai API from PHP curl.
I send options: Content-Type and Authorization (with my api key) but, when I send request, I receive this error:
Invalid Content-Type header (application/json; boundary=------------------------66a0b850cd1421c8), expected application/json
I tried to use some of your options with no success.
I cannot remove the boundary parameter added automatically.

Facebook SDK - publish post with external image

I am having hard time publishing external image to post. I am using PHP language and curl. I would like to post image (e.g. https://cdn.pixabay.com/photo/2015/06/19/17/58/sample-815141_1280.jpg) to facebook, but the only thing which get's posted is message without any image.
The problem with using photo api is that the post is posted as a picture and not as a post containing image. How can I accomplish this? I also tried with official PHP SDK but so far no luck.
$data['message'] = $_POST["content"];
$data['access_token'] = $page_access_token;
$post_url = 'https://graph.facebook.com/'.$page_id.'/feed';
if($_POST["picture"]!==""){
$data_post['url']=$_POST["picture"];
}
if($_POST["content"]!=="")
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($ch);
curl_close($ch);
}

Can not extract zip created by PHP and uploaded via cURL

I am trying to zip up a website and then upload to a CDN. But I am getting an error message when the zip is opened by the cdn: Error extracting zip: Zip end of central directory signature not found
The website is not complex, just some root level html files and an image folder. I created the zip file with PHP and when I open a downloaded copy on my computer it opens perfectly. And when I manually upload it to the cdn it extracts perfectly.
But when I upload it via PHP/cURL I get the error. So assume I am doing something wrong with my cURL functions. My apologies if this is a simplistic mistake, it is my first foray into cURL.
My upload code:
<?php
$file = "/path/to/mytest.zip";
$authorization = "Authorization: Bearer my_access_token";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://api.domain.com/bla/bla/bla");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, [$authorization, 'Content-Type: application/zip']);
$cfile = new CurlFile(#$file, 'application/zip');
$data = array('data-binary' => realpath($cfile));
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$curl_response = curl_exec($curl);
curl_close($curl);
?>
Any suggestions appreciated.
You should check this snippets on the RFC: https://wiki.php.net/rfc/curl-file-upload
The correct implementation of CurlFile is:
curl_setopt($curl_handle, CURLOPT_POST, 1);
$args['file'] = new CurlFile('filename.png', 'image/png');
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $args);

Slack. How to download image in url_private property using PHP?

I am trying to download a png image from Slack and save it into a file but the only thing that gets saved into the file is HTML code for the login page of Slack.
This is what I have tried.
$url = 'https://files.slack.com/files-pri/XXXXXX-XXXXXXX/download/2016-07-11.png';
$ch = curl_init($url);
$fp = fopen('/assets/tmp/1.png', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer xoxp-XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXXXXXXX'
));
curl_exec($ch);
curl_close($ch);
fclose($fp);
If I access the link in browser while logged into Slack, the image downloads just fine, but not through the PHP code. Any help would be appreciated.
I needed an extra permission scope to read files. I added files:read permission scope to my application from Slack Developer Console and the code works as expected.

Dropbox v2 API - large file uploads

This question follows on from my previous question on the same subject. Simple file uploads are, well, simple.
$headers = array("Authorization: Bearer dropbox_token",
'Content-Type: application/octet-stream',
'Dropbox-API-Arg: {"path":"/path/file.ext",
"mode":"add"}');
$data = "I love Stackoverflow";
$ch = curl_init('https://content.dropboxapi.com/2/files/upload/');
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
However, this is not a viable option when the file data to be save run into 10s of megabytes. I thought I would simply use the new(ish) PHP curl_file_create.
$headers = array("Authorization: Bearer Dropbox token",
'Content-Type: application/octet-stream',
'Dropbox-API-Arg: {"path":"/path/bigfile.txt",
"mode":"add"}');
$ch = curl_init('https://content.dropboxapi.com/2/files/upload/');
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
$args['file'] = curl_file_create('/path/to/bigfile.txt');
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$args);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
However, this does not work. The Dropbox API returns the message
Error in call to API function "files/upload": Bad HTTP "Content-Type" header: "application/octet-stream; boundary=--... Expecting one of "application/octet-stream", "text/plain; charset=dropbox-cors-hack
I would be most grateful to anyone who could tell me where I might be going wrong here.
It looks like curl_file_create encodes a file as a multipart form upload, which isn't what you want when talking to the Dropbox API.
If I understand correctly, the issue you're trying to address is that you don't want to load the entire file contents into memory. Is that right?
If so, please give the following a try. (Apologies that I haven't tested it at all, so it may contain mistakes.)
$headers = array('Authorization: Bearer Dropbox token',
'Content-Type: application/octet-stream',
'Dropbox-API-Arg: {"path":"/path/bigfile.txt",
"mode":"add"}');
$ch = curl_init('https://content.dropboxapi.com/2/files/upload');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
$path = '/path/to/bigfile.txt';
$fp = fopen($path, 'rb');
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($path));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
fclose($fp);
echo $response;
Note that you may also want to consider /files/upload_session_start, etc. if you're uploading large files. That lets you upload in chunks, and it supports files bigger than 150MB (which /files/upload does not).
If anyone interested a quick fix on Dropbox API v2 which CURLOPT_INFILE is not working, go to this link Dropbox PHP v2 upload issue
Greg explain and suggested to used this code curl_setopt($ch, CURLOPT_POSTFIELDS, fread($fp, $filesize)) instead of curl_setopt($ch, CURLOPT_INFILE, $fp);

Categories