duanjue2560 2018-05-03 05:30
浏览 205
已采纳

linkedin媒体上传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.

  • 写回答

1条回答 默认 最新

  • dongmubi4375 2018-05-03 06:52
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 悬赏帮写C++编程 诚信
  • ¥15 在竞速练习时输出和输入可以同时进行
  • ¥15 安卓数据提交之后格式不对
  • ¥15 需要数据库运行的图片
  • ¥15 如何获取vue-video-editor?
  • ¥100 vs2019 mfc程序如何实现64*64/48*48大小的真彩色工具栏
  • ¥15 全志v3s耳机音频输出口怎么外接功放
  • ¥15 华为ensp使用基本ACL限制公司网络访问
  • ¥15 帮我做下照片上的PLC题
  • ¥15 labview2022 使用modbus报缺少依赖?