简体   繁体   English

图像上传在Linkedin V2 API中不起作用

[英]Image upload not working in Linkedin V2 API

I'm now working with Linkedin V2 integration with my application. 我现在正在使用Linkedin V2与我的应用程序集成。 I'm facing an issue while trying to upload image to Linkedin. 在尝试将图像上传到Linkedin时,我遇到了一个问题。

I have tried CURL request from my terminal(I'm using Ubuntu OS) & getting response as below: 我已经尝试过来自终端的CURL请求 (我正在使用Ubuntu OS)并获得如下响应:

Terminal command (Working & file uploaded): 终端命令(工作和文件上传):

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'

It's response as below: 它的回复如下:

HTTP/2 201 date: Wed Apr 10 09:14:44 UTC 2019 server: Play set-cookie: lang=v=2&lang=en-us; HTTP / 2 201日期:Wed Apr 10 09:14:44 UTC 2019服务器:Play set-cookie:lang = v = 2&lang = en-us; Path=/; 路径= /; Domain=api.linkedin.com x-ambry-creation-time: Wed Apr 10 09:14:44 UTC 2019 access-control-allow-origin: https://www.linkedin.com content-length: 0 Domain = api.linkedin.com x-ambry-creation-time:Wed Apr 10 09:14:44 UTC 2019 access-control-allow-origin: https ://www.linkedin.com content-length:0

I'm facing issue when I integrate the CURL request in my application. 当我在我的应用程序中集成CURL请求时,我遇到了问题 My CURL request code from my application is given below. 我的申请中的CURL请求代码如下。

$headers = array();
$headers[] = 'Authorization: Bearer xxxxx';
$headers[] = 'X-Restli-Protocol-Version: 2.0.0';
$headers[] = 'Content-Type: multipart/form-data';
$ch = curl_init();
$options = array(
    CURLOPT_HEADER => true,
    CURLOPT_CUSTOMREQUEST => 'PUT',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => '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',
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => array("upload-file" => '/Users/peter/Desktop/superneatimage.png')
    // CURLOPT_POSTFIELDS => array("upload-file" => new CURLFile('/Users/peter/Desktop/superneatimage.png'))
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
print_r($response);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

Response as: 回复如下:

HTTP/1.1 400 Bad Request Server: Play Set-Cookie: lang=v=2&lang=en-us; HTTP / 1.1 400 Bad Request Server:Play Set-Cookie:lang = v = 2&lang = en-us; Path=/; 路径= /; Domain=api.linkedin.com Date: Wed, 10 Apr 2019 09:16:25 GMT Content-Length: 0 X-Li-Fabric: prod-lsg1 Connection: keep-alive Domain = api.linkedin.com日期:星期三,2019年4月10日09:16:25 GMT内容长度:0 X-Li-Fabric:prod-lsg1连接:keep-alive

Link to the Linkedin doc 链接到Linkedin doc

Similar question from SO , which do not solved my issue. 来自SO的类似问题 ,这并没有解决我的问题。

Use Guzzle HTTP client instead of curl.I tried curl but it is not working. 使用Guzzle HTTP客户端而不是curl.I尝试curl但它无法正常工作。

First install the composer in the current directory with below command: 首先使用以下命令在当前目录中安装composer:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

After the composer Update, run below command to install the Guzzle (default version will be 3.9 it requires php >= 5.3.3.You can specify based on your php version): 在作曲家更新之后,运行以下命令安装Guzzle(默认版本将是3.9它需要php> = 5.3.3。你可以根据你的php版本指定):

php composer.phar require guzzlehttp/guzzle php composer.phar需要guzzlehttp / guzzle

Once Guzzle client installed successfully.Please use the below code to upload the file 一旦Guzzle客户端安装成功。请使用以下代码上传文件

    require 'vendor/autoload.php';
    $client =new \GuzzleHttp\Client();
    $image_path='/path_to_your_image/my_image.png';
    $result=$client->request('PUT',$target_url, [
             'headers' => [
                 'Authorization' => 'Bearer ' . $token
              ],
              'body' => fopen($image_path, 'r'),

        ]);

You will get below success full response 您将获得以下成功的完整回复

HTTP/2 201 HTTP / 2 201

date: Wed Apr 10 09:14:44 UTC 2019 server: Play set-cookie: lang=v=2&lang=en-us; 日期:Wed Apr 10 09:14:44 UTC 2019 server:Play set-cookie:lang = v = 2&lang = en-us; Path=/; 路径= /; Domain=api.linkedin.com x-ambry-creation-time: Wed Apr 10 09:14:44 UTC 2019 access-control-allow-origin: https://www.linkedin.com content-length: 0 Domain = api.linkedin.com x-ambry-creation-time:Wed Apr 10 09:14:44 UTC 2019 access-control-allow-origin: https ://www.linkedin.com content-length:0

Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services. Guzzle是一个PHP HTTP客户端 ,可以轻松发送HTTP请求,并且可以轻松地与Web服务集成。

  • Simple interface for building query strings, POST requests, streaming large uploads, streaming large downloads, using HTTP cookies, uploading JSON data, etc... 简单的界面,用于构建查询字符串,POST请求,流式大型上传,流式大型下载,使用HTTP cookie,上传JSON数据等...
  • Can send both synchronous and asynchronous requests using the same interface. 可以使用相同的接口发送同步和异步请求。 Uses PSR-7 interfaces for requests, responses, and streams. 将PSR-7接口用于请求,响应和流。 This allows you to utilize other PSR-7 compatible libraries with Guzzle. 这允许您使用Guzzle的其他PSR-7兼容库。
  • Abstracts away the underlying HTTP transport, allowing you to write environment and transport agnostic code; 抽象出底层的HTTP传输,允许您编写环境和传输不可知代码; ie, no hard dependency on cURL, PHP streams, sockets, or non-blocking event loops. 即,没有对cURL,PHP流,套接字或非阻塞事件循环的硬依赖。
  • Middleware system allows you to augment and compose client behavior. 中间件系统允许您扩充和组合客户端行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM