Skip to content
Advertisement

Error in linkedin media upload api

I am trying to upload an image using linkedin api. I already uploaded image in my server and now I want to upload the upoaded image url to linkedin. But I got the error

{ “serviceErrorCode”:0,”message”:”The request is not a multipart request”,”status”:400 }

      $curl1 = curl_init();
      $m_url="https://api.linkedin.com/media/upload";

      $curl_header = array("Authorization:Bearer ".$access_token);
      $uploadRequest = array(
            'name'=>'media',
            'filename' => basename($medias[0]),
            'media_data' => base64_encode(file_get_contents($medias[0]))
                );
                curl_setopt_array($curl1, array(
                  CURLOPT_URL => $m_url,
                  CURLOPT_RETURNTRANSFER => true,
                  CURLOPT_ENCODING => "",
                  CURLOPT_MAXREDIRS => 10,
                  CURLOPT_TIMEOUT => 30,
                  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                  CURLOPT_CUSTOMREQUEST => "POST",
                  CURLOPT_POSTFIELDS =>$uploadRequest,
                  CURLOPT_HTTPHEADER=>$curl_header,

                ));
                $response1 = curl_exec($curl1);
                $err1 = curl_error($curl1);

                curl_close($curl1);

This is the request. Please anyone tell me What is the exact issue and how to sort this out.Thanks in advance.

Advertisement

Answer

Added header like this:

       $curl_header=array("Content-Type:multipart/form-data", 
       "Authorization:Bearer ".$access_token);

And changed the post params like this

          $curl1 = curl_init();

          $m_url="https://api.linkedin.com/media/upload";
          $uploadRequest['file'] = new CURLFile($img, 'image/png', basename($img));


            $curl_header=array("Content-Type:multipart/form-data", "Authorization:Bearer ".$access_token);

                curl_setopt_array($curl1, array(
                  CURLOPT_URL => $m_url,
                  CURLOPT_RETURNTRANSFER => true,
                  CURLOPT_ENCODING => "",
                  CURLOPT_MAXREDIRS => 10,
                  CURLOPT_TIMEOUT => 30,
                  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                  CURLOPT_CUSTOMREQUEST => "POST",
                  CURLOPT_POSTFIELDS =>$uploadRequest,
                  CURLOPT_HTTPHEADER=>$curl_header,

                ));

                $response1 = curl_exec($curl1);
                $err1 = curl_error($curl1);
                $status1 = curl_getinfo($curl1, CURLINFO_HTTP_CODE);


                curl_close($curl1);
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement