Square Connect API - image upload - Empty Reply From Server code 52 error

前端 未结 1 1961
青春惊慌失措
青春惊慌失措 2021-01-28 05:36

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

Trie

相关标签:
1条回答
  • 2021-01-28 05:55

    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'
        )
    );
    ?>
    
    0 讨论(0)
提交回复
热议问题