如何将带有"

编程入门 行业动态 更新时间:2024-10-11 13:24:37
本文介绍了如何将带有"-i --upload-file"的curl调用转换为PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

此调用在docs 此处

我正在关注文档,以通过第2版API在LinkedIn中创建图像和文本共享.按照文档中的步骤,我成功完成了每个步骤.但是,我的帖子在LinkedIn中不存在,并且无法基于API请求返回的ID通过直接链接查看

I am following the docs to create an image and text share in LinkedIn through the version 2 API. Following the steps in the docs, I completed each step with a successful response. However, my post does not exist in LinkedIn, and cannot be viewed by direct link based on the ID returned by the API request

在调试中,我向 api.linkedin/v2 /assets/C5622AQFbyxMfU2b9zg .这是为了检索媒体资产(文档此处)在第一步中创建.它返回状态为"CLIENT_ERROR"的以下json.不过,如果您查看文档,则此状态不是该字段列出的状态之一

In debuging I made a get request to api.linkedin/v2/assets/C5622AQFbyxMfU2b9zg. This is to retrieve the media asset (docs here) created in the first step. It returned the following json with the 'CLIENT_ERROR' status. If you look at the docs though, this status is not one of those listed for the field

{ "serviceRelationships": [ { "identifier": "urn:li:userGeneratedContent", "relationshipType": "OWNER" } ], "recipes": [ { "recipe": "urn:li:digitalmediaRecipe:feedshare-image", "status": "CLIENT_ERROR" } ], "mediaTypeFamily": "STILLIMAGE", "created": 1550875198973, "lastModified": 1550875199857, "id": "C5622AQFbyxMfU2b9zg", "status": "ALLOWED" }

我能够使用命令行curl成功上传文件.看来问题出在我的PHP实现上.具体来说,二进制文件的上传无法正常进行.这是我的php代码的简化表示.

I was able to successfully upload the file using the command line curl. It seems the issue is with my PHP implementation. Specifically the upload of the binary file is not working right. Here is a simplified representation of my php code.

$ch = curl_init(); $headers = [ 'Authorization: Bearer ' . $this->accessToken->access_token, 'Cache-Control: no-cache', 'X-RestLi-Protocol-Version: 2.0.0', 'x-li-format: json', 'Content-Type: ' . $this->mimetype ]; $data = [ 'file' => curl_file_create($filename, $this->mimetype); ]; curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_URL, 'api.linkedin/mediaUpload/C5622AQHSLBWkZeg-gQ/feedshare-uploadedImage/0?ca=vector_feedshare&cn=uploads&m=AQJIFvVUxXl7yAAAAWkwjT0kEPKHY5BnAgXETcVQ3AbsxmvaGl6hnuECwA&app=22961836&sync=0&v=beta&ut=2RaKRdHD_OwUE1'); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($ch); print_r($result);

推荐答案

我最近也遇到过同样的问题,linkedin的文档不是很有帮助. 创建图像共享.

I ran into this same issue recently and linkedin's documents were not very helpful. Create an Image Share.

首先要了解的是"curl --upload-file"使用PUT请求方法.这是我使用伪数据使用的.

The first thing to understand is that "curl --upload-file" uses a PUT request method. Here is what I used using dummy data.

$file = "/storage/media/testimg.jpg"; $uploadurl = "api.linkedin/mediaUpload/C5622AQHSLBWkZeg-gQ/feedshare-uploadedImage/0?ca=vector_feedshare&cn=uploads&m=AQJIFvVUxXl7yAAAAWkwjT0kEPKHY5BnAgXETcVQ3AbsxmvaGl6hnuECwA&app=22961836&sync=0&v=beta&ut=2RaKRdHD_OwUE1"; $accesstoken = "acacdsv13rv31bv13b1cf13cf13rv13rv13vr13tb5dxvghn"; $curl_handle=curl_init(); curl_setopt($curl_handle, CURLOPT_URL, $uploadurl); curl_setopt($curl_handle, CURLOPT_PUT, 1); curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, "PUT"); $fh_res = fopen($file, 'r'); curl_setopt($curl_handle, CURLOPT_INFILE, $fh_res); curl_setopt($curl_handle, CURLOPT_INFILESIZE, filesize($file)); curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); $headers[] = "Authorization: Bearer $accesstoken"; //linkedin says to add 'redacted' instead of access token (that's bullshit) $headers[] = 'Connection: Keep-Alive'; $headers[] = "X-RestLi-Protocol-Version:2.0.0"; curl_setopt($curl_handle,CURLOPT_HTTPHEADER,$headers); $rcurl = curl_exec($curl_handle); curl_close($curl_handle);

如果转储响应$rcurl,则只会得到一个空字符串.因此,下一步就是检查上传状态,他们完全忘记了这些内容作为图像共享文档的一部分,但是可以在此处找到查看支持状态

If you dump the response $rcurl you only get an empty string. So the next step is to check the status of the upload, something they completely forgot to mention as part of the image share documentation but can be found here Check Status of Uphold

使用来自初始图像注册调用的资产变量,您检查上传的状态,直到状态检查返回AVAILABLE.

Using the asset variable from the initial image register call, you check the upload's status until the status check returns AVAILABLE.

$asset = "urn:li:digitalmediaAsset:C4asdfvqqrv3"; $a = explode("digitalmediaAsset:", $asset); $statuscheck = make a GET curl call to the url: "/assets/" . $a[1] $uploadstat = $statuscheck['recipes'][0]['status']; do{ $statuscheck = make a GET curl call to the url: "/assets/" . $a[1]; $uploadstat = $statuscheck['recipes'][0]['status']; }while($uploadstat != 'AVAILABLE');

一旦返回AVAILABLE,您现在就可以继续进行下一步,以创建图像共享.希望这可以帮助.

Once AVAILABLE is returned, you can now proceed to the next step of creating the image share. Hope this helps.

更多推荐

如何将带有"

本文发布于:2023-10-10 22:17:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479914.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!