0

Trying to get square connect API image upload working using PHP.

I used the square connect API guide: docs.connect.squareup.com/api/connect/v1/#post-image

Tried two different ways based on what I found on StackOverflow and Google searching.

Method 1) regular curl request: https://gist.github.com/delalis/17c3c111e3b42df127ed

Method 2) using CURLFile (php >=5.5 only) https://gist.github.com/delalis/5c7ecc2aaa024927b360

Both methods gave me this empty reply from server error:

Error: "Empty reply from server" - Code: 52

I am able to connect to square to do other functions no problem, image uploading however is proving to be quite difficult!

Any help would be greatly appreciated.

T_C
  • 1
  • 1

1 Answers1

0

Here's a working PHP example that I usually share with people: https://gist.github.com/tdeck/7118c7128a4a2b653d11

<?php
function uploadItemImage($url, $access_token, $image_file) {
    $headers = ["Authorization: Bearer $access_token"];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, ['image_data' => "@$image_file;type=image/jpeg"]);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($ch);
    $return_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    print "POST to $url with status $return_status\n";
    curl_close($ch);
    return $data ? json_decode($data) : false;
}
print_r(
    uploadItemImage(
        'https://connect.squareup.com/v1/me/items/ITEM_ID/image',
        'ACCESS_TOKEN',
        'IMAGE_FILE.jpg'
    )
);
?>
Troy
  • 1,599
  • 14
  • 28
  • Thanks Troy. Finally got it working using this example... Also wondering if it is possible to BATCH image upload requests? I tried to do it, it gives this error: "The Content-Type of this request is not supported. Supported type(s): multipart/form-data"........ I guess that makes sense. So question remains: is it possible? Or should I try using a one or two second timeout between image upload requests. – T_C Jul 13 '15 at 13:50
  • Unfortunately the batch API doesn't work for this particular endpoint. I would suggest just making individual requests. The rate limits aren't fixed, but I would try to not make more than 100 requests per minute. If you get a 429 from the server, you'll need to pull back. – Troy Jul 13 '15 at 23:06