1

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.

1
  • 1
    They do not want you to send the path (what would the API be supposed to do with a local path value you provide?), but the actual file content. filename=@<IMAGE_PATH> with the @ in there is special cURL syntax meaning "make this a file upload."
    – CBroe
    Jul 5, 2022 at 10:57

2 Answers 2

0

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'],);
4
  • Thanks for response. I know there is SDK to use but I don't want to use ready packages so much. Instead I am trying to make my own logic. If you have solution for my logic let me know. Jul 5, 2022 at 11:29
  • 1
    I am not sure, but it seems access_token needs to be passed as query param. --> graph.facebook.com/v14.0/me?fields&access_token=<access token sanitized> - From doc example Jul 5, 2022 at 11:42
  • Thanks for saving my time Alexander. You are right that was request issue. I had to attach filename to my request in Http Client. access_token need to be passed as query param. Jul 5, 2022 at 11:57
  • Glad to help you :) Jul 5, 2022 at 13:17
0

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()));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.