square/connect-php-sdk

CreateCatalogImageRequest Model belong to what API?

Closed this issue · 7 comments

This may be a stupid question but what is the API for CreateCatalogImageRequest Model?

Can somebody post an example of creating a new image before I lose my mind?

Hey @zamliyconsulting this SDK doesn’t support file upload endpoints yet, so you’ll have to manually call CreateCatalogImage using an HTTP client, like Guzzle.

@deanpapastrat thanks so much. Is there any way you guys can post an example for php?

Meanwhile, did you know that "Create catalog image" call using the API Explorer on Square Developer site throws an error:
{
"errors": [
{
"category": "INVALID_REQUEST_ERROR",
"code": "INVALID_CONTENT_TYPE",
"detail": "Only [multipart/form-data] content type allowed. but got application/json"
}
]
}

Yep, unfortunately it’s a bug on our end (the functionality to view that endpoint in the API Explorer isn’t finished yet, and the button to view it shouldn’t show). However, we’ll be launching full support for CreateCatalogImage in the API Explorer later this week, so it’ll be there soon!

I can try and create a PHP sample for you in a bit - to be honest, I’m a bit rusty, but I can probably get some type of kinda-working-code that a person who’s better versed in PHP would be able to figure out. Sound good? 😊

Using Guzzle, I think it'd look something like this:

use GuzzleHttp\Client;

$client = new Client([
  // Use https://connect.squareupsandbox.com for sandbox
  'base_uri' => 'http://connect.squareup.com'
]);

$request_data = json_encode([
  'idempotency_key' => 'some unique key',
  'object_id' => "CatalogItem ID",
  'image' => [
      'id' => '#TEMP_ID',
      'type' => 'IMAGE',
      'image_data' => [
        'name' => 'My picture'
        'caption' => 'A picture of a cup of coffee'
      ]
  ]
]);

$response = $client->request('POST', '/v2/catalog/images', [
  'headers' => [
    'Accept' => 'application/json',
    'Authorization' => 'Bearer ACCESS_TOKEN',
    'Square-Version' => '2020-04-22'
  ],
  'multipart' => [
    [
      'name' => 'request',
      'contents' => $request_data
    ],
    [
      'name' => 'file',
      'contents' => fopen('/path/to/file', 'r')
    ]
  ]
]);

echo $response->getBody();

Hopefully that helps!

@deanpapastrat thank you so much, this was a life saver.. I had to make some minor corrections. The below version works fine.

$client = new \GuzzleHttp\Client([
	  'base_uri' => 'https://connect.squareup.com'
	]);

	$request_data = json_encode([
	  'idempotency_key' => 'X'. rand(),
	  'object_id' => $square_item_id,
	  'image' => [
		  'id' => '#TEMP_ID',
		  'type' => 'IMAGE',
		  'image_data' => [
			'caption' => 'Hio Seletti Crab',
			'name' => 'Hio Seletti Crab',
		  ]
	  ]
	]);

	try {
		$response = $client->request('POST', '/v2/catalog/images', [
		  'headers' => [
			'Accept' => 'application/json',
			'Authorization' => 'Bearer '. $access_token,
			'Cache-Control' => 'no-cache',
			'Square-Version' => '2020-04-22'
		  ],
		  'multipart' => [
			[
			  'name' => 'request',
			  'contents' => $request_data
			],
			[
			  'name' => 'file',
			  'contents' => fopen($image_url, 'r')
			]
		  ]
		]);
	} catch (Exception $e) {
		echo 'Exception with image update ', $e->getMessage(), PHP_EOL;
	}

Great! Sorry it wasn't right the first time, but glad you could make it work. Going to close this as resolved.