0

I'm using Linkedin API from https://learn.microsoft.com/en-us/linkedin/marketing/integrations/community-management/shares/vector-asset-api?tabs=http#upload-the-image and I get the uploadUrl after registering the image. But I receive null when I want to upload the image. this is my code with php:

$body = file_get_contents('C:/Users/HH/Desktop/test.jpg');

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,            "https://api.linkedin.com/mediaUpload/C4D22AQFyt9bYpBuRcw/feedshare-uploadedImage/0?ca=vector_feedshare&cn=uploads&m=AQIUeWqFh419_gAAAXyYlBzYF8LNZVjHdjGaH103jP998K1bpxeJPGGJtw&app=82730583&sync=1&v=beta&ut=1ccVodv_trJFY1" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST,           1 );
curl_setopt($ch, CURLOPT_POSTFIELDS,     $body ); 
curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Type: text/plain','Authorization: Bearer ' . $token)); 

$result=curl_exec ($ch);
var_dump($result);
Hoda Kh
  • 357
  • 3
  • 19
  • "text/plain" definitely isn't the content type of an image! And `$body` is just a file _name_, not the _content_ of the file. I suggest researching how to upload a file with curl/PHP – ADyson Oct 19 '21 at 13:15
  • I use file_get_contents for $body and it is not just the name. I have searched a lot and I used different samples but all of them return null. – Hoda Kh Oct 19 '21 at 13:24
  • 1
    Apologies you're right about that, I misread it. But text/plain is still wrong. Clearly an image file is not text. Normally a file gets uploaded with a multipart/form-data content-type – ADyson Oct 19 '21 at 13:25
  • 1
    `all of them return null`... what result are you expecting? According to that documentation, the endpoint will return a HTTP 201 response on success, with various _headers_, but it says nothing about any content in the response body. You would need to check the response headers as well to be sure what's happened. See https://stackoverflow.com/questions/9183178/can-php-curl-retrieve-response-headers-and-body-in-a-single-request if you need guidance on that. – ADyson Oct 19 '21 at 13:31
  • @ADyson Thank you. I receive http code 400. I will check headers. – Hoda Kh Oct 19 '21 at 13:36
  • I note also that the documentation says to use a PUT request for this, but your curl is sending a POST – ADyson Oct 19 '21 at 13:42
  • I solved it with changing the content-type to application/binary – Hoda Kh Oct 19 '21 at 13:42

0 Answers0