【发布时间】:2021-03-07 23:21:48
【问题描述】:
我无法附加上传的文件。我看到 fopen($image->getPathname(),'r') 返回不可转换的 json 数据类型,使 guzzle 返回错误 json_encode 错误:不支持类型。我在这里做错了什么?谢谢。
stream resource @586 ▼
timed_out: false
blocked: true
eof: false
wrapper_type: "plainfile"
stream_type: "STDIO"
mode: "r"
unread_bytes: 0
seekable: true
uri: "\Temp\php2D41.tmp"
options: []
//use Illuminate\Support\Facades\Http;
//use Illuminate\Support\Facades\File;
if($request->hasFile('imgInp1'))
{
foreach(request('imgInp1') as $image)
{
$message = Http::post('https://graph.facebook.com/v9.0/me/message_attachments?access_token='.$access_token,
[
'headers' => [
'Content-Type' => 'multipart/form-data',
],
'multipart' => [
'name' => 'image',
'image_path' => $image->getPathname(),
'image_mime' => $image->getmimeType(),
'image_org' => $image->getClientOriginalName(),
'contents' => fopen($image->getPathname(), 'r'),
],
'message'=>[
'attachment' => [
'type' => 'image',
'payload' => [
"is_reusable"=> true,
],
],
],
]);
}
}
【问题讨论】:
-
您确定在多部分帖子中需要
attachment数组吗? -
multipart中使用的key也只有4个,name,contents,header,filename
-
我猜你正在使用本网站developers.facebook.com/docs/messenger-platform/reference/… 上列出的 api,你正在使用 multipart 它会将数据作为 multipart/form-data 发送,** 错误** 你正在将包装器 httpclient 的语法与直接使用 guzzle,** 错误** 如果您使用的是 multipart,则应用程序数据进入 multipart 您使用消息键将其分隔
-
是的,我正在尝试翻译从文件上传的 curl -F 'message={"attachment":{"type":"image", "payload":{"is_reusable":true}}}' -F 'filedata=@/tmp/shirt.png;type=image/png' 到 http 客户端/guzzle。我的错误是上传的文件数量不正确。我想我没有正确附加图像文件...我似乎无法让 fopen 为 json 编码工作...我已经将 content-type 更改为 'application/json'
-
是什么让您认为通过 guzzle 上传文件可以通过传递文件 pointer 作为参数来工作?我猜这应该是此时传递的实际文件 content,因此此时读取该文件的快速方法是
file_get_contents。
标签: php laravel facebook-graph-api guzzle