0

I want to create a product on my website and have it be created on square (which is working). However I also want to set the initial inventory which is seems there is no way to do it from the documentation. https://docs.connect.squareup.com/api/connect/v1/#post-inventory-variationid

If I go into my square account I can manually set up an initial amount, then query that entry and get the id and update it, but who wants to do anything manually. It defeats the purpose. Is there a way to create an inventory entry?

My second struggle is with uploading an image using unirest.

function uploadItemImage($itemId, $image_file) 
{
    global $accessToken, $locationId, $connectHost; 
    $requestHeaders = array 
    (
      'Authorization' => 'Bearer ' . $accessToken,
      'Accept' => 'application/json',
      'Content-Type' => 'multipart/form-data;'
    );  
    $request_body = array
    (   
'image_data'=>Unirest\Request\Body::file($image_file, 'text/plain', myproduct.jpg')
    );
    $response = Unirest\Request::post($connectHost . '/v1/' . $locationId . '/items/'.$itemId.'/image', $requestHeaders, $request_body);
    print(json_encode($response->type, JSON_PRETTY_PRINT)); 
}

where $itemId is taken from the product created earlier and $image_file is the direct link to the file on my server

I keep getting this error...

> PHP Fatal error:  Uncaught exception 'Unirest\Exception' with message
> 'couldn't open file "https://somewebsite/myPicture.jpg"  ' in
> rootFolder/Unirest/Request.php:479  Stack trace: 
> #0 rootFolder/Unirest/Request.php(292): Unirest\Request::send('POST', 'https://connect...', Array, Array, NULL, NULL) 
> #1 rootFolder/

Any help is much appreciated!

ManuEng13
  • 1
  • 1

1 Answers1

0

Way to maximise the use of your question!

  1. There is not currently a way to set initial inventory via API, but new item and inventory management APIs are in the works, read more on the Square Blog
  2. I'm assuming that you are not literally using "https://somewebsite/myPicture.jpg" but it seems like unirest thinks you are trying to use a web url instead of getting a file from your filesystem. Try the following curl command and see if you can match up all the parts to unirest:

:)

curl --request POST \
--url https://connect.squareup.com/v1/XXXXXX/items/XXXXX/image \ 
--header 'authorization: Bearer sq0atp-XXXXX' \
--header 'cache-control: no-cache' \
--header 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
--form image_data=@/Users/ManuEng13/Desktop/test.png
tristansokol
  • 4,054
  • 2
  • 17
  • 32
  • 1. Bummer, can't wait for that to come out. I'm following your blog now. 2. I still can't get a picture upload to work with UNIREST. I think I'm getting close however I did get it to work with [link](http://stackoverflow.com/questions/31349302/square-connect-api-image-upload-empty-reply-from-server-code-52-error) but interestingly, I can only get it to work if it is jpeg, not jpg and it has to be in the same folder. The jpg thing is just a pain but I really need to be able to upload images from remote servers. Is there a block on that from square? – ManuEng13 Apr 07 '17 at 16:07
  • no, the endpoint only cares about the image data being serialized, and not passing in a link. I'm not sure how Unirest works, but there is nothing stopping you from having a script that reads a remote url for an image and then passes the data to Square. – tristansokol Apr 07 '17 at 21:22