question

smerchanton avatar image
smerchanton asked bryanvargas edited

Associating Items with Categories PHP

I am trying to link items with categories and I am having trouble using Php. I am able to submit a curl post item and get it to work but when I am using the GuzzleHttp method, it is not working.

In curl I use:


$curl=curl_init('https://sandbox.dev.clover.com/v3/merchants/{MID}/category_items');

curl_setopt($curl, CURLOPT_HTTPHEADER, array(

        "Authorization:Bearer {access token}",
        'Content-Type: application/json',
    )
);

$data='{"elements":[{"category":{"id":"category_id"},"item":{"id":"item_id"}}]}';

curl_setopt( $curl, CURLOPT_POST, true );
curl_setopt( $curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$auth = curl_exec($curl);

This works properly.

With guzzlehttp I am using this method but it is not working. I use similar methods for creating an Item and it works fine but on this method for linking item and category I am not able to get it working. I am not getting an error but I am also not getting the item and category link. What am I doing wrong?

$client = new \GuzzleHttp\Client();

$data='{"elements":[{"category":{"id":"category_id"},"item":{"id":"item_id"}}]}';

$response = $client->request('POST', 'https://sandbox.dev.clover.com/v3/merchants/{MID}/category_items', [
    'data' => $data,
    'headers' => [
        'authorization' => 'Bearer {access token}',
        'content-type' => 'application/json',
    ],
]);
REST API
1 comment
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

bryanvargas avatar image bryanvargas ♦♦ commented ·

Hey, I tested this endpoint with your code but changed 'data' to 'body'. After I made that change and ran the code, the category was added to the item. However, the response will be an empty returned response, but the merchant dashboard will show that the category was added to the item ( or you can use the GET /v3/merchants/{mId}/items/{itemId}/categories to get all categories of a single item). I tested this also in POSTMAN with the same results (empty response but change was made): Here is my code reference:



<?php
require_once( "/vendor/autoload.php");
  
use GuzzleHttp\Client;
try{
$client = new Client();
$data='{"elements":[{"category":{"id":"{catId}"},"item":{"id":"{itemId}"}}]}';


$response = $client->request('POST', 'https://sandbox.dev.clover.com/v3/merchants/{mid}/category_items', [
    'body' => $data,
    'headers' =>[
        'authorization'=> 'Bearer {accessToken}',
        'content-type' => 'application/json'
    ],
]);
 if(200 == $response ->getStatusCode()){
    $response = $response->getBody();
    $arr_result = json_decode($response);
    print_r($arr_result);
 }} catch (\Exception $e){
    echo $e->getMessage();
 }


1 Like 1 ·

1 Answer

·
Shoviv EML Converter avatar image
Shoviv EML Converter answered bryanvargas edited

In PHP, there are several ways to associate items with categories. Here are a few examples:

Using Arrays: You can create an array where each key represents a category and the values represent the items associated with that category. For example:

$items = array(

'Category A' => array('Item 1', 'Item 2', 'Item 3'),

'Category B' => array('Item 4', 'Item 5', 'Item 6'),

'Category C' => array('Item 7', 'Item 8', 'Item 9')

);


Using Objects: You can create an object where each property represents a category and the values represent the items associated with that category. For example:

class Categories {

public $categoryA = array('Item 1', 'Item 2', 'Item 3');

public $categoryB = array('Item 4', 'Item 5', 'Item 6');

public $categoryC = array('Item 7', 'Item 8', 'Item 9');

}


$categories = new Categories();


Using a Database: You can create a database table with two columns, one for the category and one for the item. Each row represents an association between an item and a category. For example:

CREATE TABLE items (

id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,

category VARCHAR(30) NOT NULL,

item VARCHAR(30) NOT NULL

);


INSERT INTO items (category, item)

VALUES ('Category A', 'Item 1'), ('Category A', 'Item 2'), ('Category A', 'Item 3'),

('Category B', 'Item 4'), ('Category B', 'Item 5'), ('Category B', 'Item 6'),

('Category C', 'Item 7'), ('Category C', 'Item 8'), ('Category C', 'Item 9');


Thnaks


10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Welcome to the
Clover Developer Community