我正在尝试使用图形api将图像上传到facebook。我想将图像作为文件提交,而不是提供URL,这是另一种选择。我使用PHP Laravel和guzzle来实现这一点。不幸的是,当我上传文件时,我收到了一个错误,似乎表明图像未被识别。
这是我使用的文档
https://developers.facebook.com/docs/graph-api/reference/page/photos/#publish
这是我的错误信息
message: "Client error: `POST https://graph.facebook.com/v12.0/99999999999/photos` resulted in a `400 Bad Request` response:n{"error":{"message":"(#324) Requires upload file","type":"OAuthException","code":324,"fbtrace_id":"ACxilINTWYdb3wGOXfGg7 (truncated...)n"
这是我的代码
public function media(Request $request)
{
$facebookPageConnection = FacebookPageConnection::find(2);
$file = $request->file('file');
$client = new Client();
$body = $client->post("https://graph.facebook.com/v12.0/$facebookPageConnection->facebook_page_id/photos", [
'multipart' => [
[
'name' => 'message',
'contents' => 'test post'
],
[
'name' => 'source',
'contents' => base64_encode(file_get_contents($file))
],
[
'name' => 'access_token',
'contents' => $facebookPageConnection->access_token
]
]
])->getBody();
return json_decode($body);
}
我的错误是使用
file_get_contents
和
base64_encode
相反,为了让它发挥作用,我需要使用fopen
$body = $client->post("https://graph.facebook.com/v12.0/$facebookPageConnection->facebook_page_id/photos", [
'multipart' => [
[
'name' => 'message',
'contents' => 'test post'
],
[
'name' => 'source',
'contents' => fopen($file, 'rb')
],
[
'name' => 'access_token',
'contents' => $facebookPageConnection->access_token
]
]
])->getBody();