提供的图像文件无效。请再次检查您的图像路径。 Facebook Graph API错误

发布于 2025-02-13 01:29:51 字数 971 浏览 1 评论 0原文

我正在尝试将图像与Laravel的HTTP客户端上传到Facebook Graph API。但是我之前提到的错误。

    $ad_account_id = env("AD_ACCOUNT_ID");
    $access_token = env("ACCESS_TOKEN");
    $image = $request->file('image');

    $response = Http::asForm()
        ->withHeaders(['Content-Type: multipart/form-data'])
        ->post('https://graph.facebook.com/v14.0/act_' . $ad_account_id . '/adimages',
            [
                'filename' => file_get_contents($image),
                'access_token' => $access_token
            ]
        );

    dd(json_decode($response->body()));

在文档中,Facebook为我提供了这样的卷曲API示例,例如此

curl \
  -F 'filename=@<IMAGE_PATH>' \
  -F 'access_token=<ACCESS_TOKEN>' \
  https://graph.facebook.com/v<API_VERSION>/act_<AD_ACCOUNT_ID>/adimages

问题与Image_path有关。我尝试以Base64格式发送任何上传图像的路径。我可以在Postman中使用同一API上传同一图像。关于access_tokenad_account_id没有任何问题。

I am trying to upload image to Facebook graph api with Http Client of Laravel. But I am getting the error mentioned before.

    $ad_account_id = env("AD_ACCOUNT_ID");
    $access_token = env("ACCESS_TOKEN");
    $image = $request->file('image');

    $response = Http::asForm()
        ->withHeaders(['Content-Type: multipart/form-data'])
        ->post('https://graph.facebook.com/v14.0/act_' . $ad_account_id . '/adimages',
            [
                'filename' => file_get_contents($image),
                'access_token' => $access_token
            ]
        );

    dd(json_decode($response->body()));

In documentation Facebook gives me curl api example like this

curl \
  -F 'filename=@<IMAGE_PATH>' \
  -F 'access_token=<ACCESS_TOKEN>' \
  https://graph.facebook.com/v<API_VERSION>/act_<AD_ACCOUNT_ID>/adimages

Problem is all about IMAGE_PATH. I have tried to send any kind of path of uploaded image even in base64 format. I am able to upload the same image with same api in Postman. There is not any problem about access_token or ad_account_id.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绅刃 2025-02-20 01:29:51

建议将PHP SDK用于此类操作。
https://github.com/faceblebook/face-pace-phpbebook-php-php-business-sdk


    $account = new \FacebookAds\Object\AdAccount('Your ID');
    assert($account instanceof \FacebookAds\Object\AdAccount);
    $account->createAdImage(['array of fields'],['array of request Params'],);

Will recommend to use PHP SDK for such operations.
https://github.com/facebook/facebook-php-business-sdk


    $account = new \FacebookAds\Object\AdAccount('Your ID');
    assert($account instanceof \FacebookAds\Object\AdAccount);
    $account->createAdImage(['array of fields'],['array of request Params'],);
悲凉≈ 2025-02-20 01:29:51

通过使用附加 http客户端的方法解决了我的问题:

$ad_account_id = env("AD_ACCOUNT_ID");
$access_token = env("ACCESS_TOKEN");
$image = $request->file('image');

$response = Http::attach('filename', file_get_contents($image), 'image.jpg')
    ->post('https://graph.facebook.com/v14.0/act_' . $ad_account_id . '/adimages',
        [
            'access_token' => $access_token
        ]
    );

dd(json_decode($response->body()));

Solved my problem by using attach method of HTTP client:

$ad_account_id = env("AD_ACCOUNT_ID");
$access_token = env("ACCESS_TOKEN");
$image = $request->file('image');

$response = Http::attach('filename', file_get_contents($image), 'image.jpg')
    ->post('https://graph.facebook.com/v14.0/act_' . $ad_account_id . '/adimages',
        [
            'access_token' => $access_token
        ]
    );

dd(json_decode($response->body()));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文