0

I want to convert the xml data that I have generated from my system into an array array and send it to the API system of the platform I sell individually.

I'd appreciate it if you could review and support.

my array data example:

Array
(
    [product] => Array
        (
            [0] => Array
                (
                    [productSellerCode] => bebe27
                    [title] =>  Uzun Kollu Renkli Uyku Tulum Body Zıbın 6 Aylık Bebek
                    [subtitle] => Stoktan hızlı kargo
                    [description] => Bebeğiniz İçin Özel Günlerde ve Günlük Olarak Kullanabileceği Mükemmel Takım.Kaliteli Şık ve Zarif Olan Bu Ürünü Sizlere Sunuyoruz.%100 Pamuk olan bu ürünü Bebeğinize Güvenle Giydirebilirsiniz Dokusu Yumuşaktır.
                    [category] => Array
                        (
                            [id] => 1000053
                        )

                    [price] => 48.472
                    [domestic] => false
                    [currencyType] => 1
                    [images] => Array
                        (
                            [image] => Array
                                (
                                    [url] => https://tuccar.com.tr/image/catalog/bebe/IMG_7976.JPG
                                    [order] => 1
                                )

                        )

                    [attributes] => Array
                        (
                            [attribute] => Array
                                (
                                    [0] => Array
                                        (
                                            [name] => Beden
                                            [value] => 3-6 Ay
                                        )

                                    [1] => Array
                                        (
                                            [name] => Marka
                                            [value] => DİĞER
                                        )

                                )

                        )

                    [saleStartDate] => Array
                        (
                        )

                    [saleEndDate] => Array
                        (
                        )

                    [productionDate] => Array
                        (
                        )

                    [expirationDate] => Array
                        (
                        )

                    [productCondition] => 1
                    [preparingDay] => 2
                    [discount] => Array
                        (
                            [startDate] => Array
                                (
                                )

                            [endDate] => Array
                                (
                                )

                            [type] => Array
                                (
                                )

                            [value] => Array
                                (
                                )

                        )

                    [shipmentTemplate] => kargo-bizden
                    [stockItems] => Array
                        (
                            [stockItem] => Array
                                (
                                    [quantity] => 1
                                    [sellerStockCode] => Array
                                        (
                                        )

                                    [attributes] => Array
                                        (
                                            [attribute] => Array
                                                (
                                                    [name] => Array
                                                        (
                                                        )

                                                    [value] => Array
                                                        (
                                                        )

                                                )

                                        )

                                    [optionPrice] => Array
                                        (
                                        )

                                )

                        )

                )

            [1] => Array
                (
                    [productSellerCode] => MS-S010
                    [title] => 10 cm Samur Kase
                    [subtitle] => Stoktan hızlı kargo
                    [description] => Ölçü: 10 cmÜlkemizde, el işçiliği şeklinde üretilmiştir.
                    [category] => Array
                        (
                            [id] => 1016108
                        )

                    [price] => 31.426
                    [domestic] => false
                    [currencyType] => 1
                    [images] => Array
                        (
                            [image] => Array
                                (
                                    [url] => https://tuccar.com.tr/image/catalog/MS/MS-S009-1.jpg
                                    [order] => 1
                                )

                        )

                    [attributes] => Array
                        (
                            [attribute] => Array
                                (
                                    [0] => Array
                                        (
                                            [name] => Marka
                                            [value] => DİĞER
                                        )

                                    [1] => Array
                                        (
                                            [name] => Materyal
                                            [value] => Seramik
                                        )

                                )

                        )

                    [saleStartDate] => Array
                        (
                        )

                    [saleEndDate] => Array
                        (
                        )

                    [productionDate] => Array
                        (
                        )

                    [expirationDate] => Array
                        (
                        )

                    [productCondition] => 1
                    [preparingDay] => 3
                    [discount] => Array
                        (
                            [startDate] => Array
                                (
                                )

                            [endDate] => Array
                                (
                                )

                            [type] => Array
                                (
                                )

                            [value] => Array
                                (
                                )

                        )

                    [shipmentTemplate] => kargo-bizden
                    [stockItems] => Array
                        (
                            [stockItem] => Array
                                (
                                    [quantity] => 10
                                    [sellerStockCode] => Array
                                        (
                                        )

                                    [attributes] => Array
                                        (
                                            [attribute] => Array
                                                (
                                                    [name] => Array
                                                        (
                                                        )

                                                    [value] => Array
                                                        (
                                                        )

                                                )

                                        )

                                    [optionPrice] => Array
                                        (
                                        )

                                )

                        )

                )

I want to post an array block here every time.

(
    [productSellerCode] => MS-S010
    [title] => 10 cm Samur Kase
    [subtitle] => Stoktan hızlı kargo
    [description] => Ölçü: 10 cmÜlkemizde, el işçiliği şeklinde üretilmiştir.
    [category] => Array
        (
            [id] => 1016108
        )

    [price] => 31.426
    [domestic] => false
    [currencyType] => 1
    [images] => Array
        (
            [image] => Array
                (
                    [url] => https://tuccar.com.tr/image/catalog/MS/MS-S009-1.jpg
                    [order] => 1
                )

        )

    [attributes] => Array
        (
            [attribute] => Array
                (
                    [0] => Array
                        (
                            [name] => Marka
                            [value] => DİĞER
                        )

                    [1] => Array
                        (
                            [name] => Materyal
                            [value] => Seramik
                        )

                )

        )

    [saleStartDate] => Array
        (
        )

    [saleEndDate] => Array
        (
        )

    [productionDate] => Array
        (
        )

    [expirationDate] => Array
        (
        )

    [productCondition] => 1
    [preparingDay] => 3
    [discount] => Array
        (
            [startDate] => Array
                (
                )

            [endDate] => Array
                (
                )

            [type] => Array
                (
                )

            [value] => Array
                (
                )

        )

    [shipmentTemplate] => kargo-bizden
    [stockItems] => Array
        (
            [stockItem] => Array
                (
                    [quantity] => 10
                    [sellerStockCode] => Array
                        (
                        )

                    [attributes] => Array
                        (
                            [attribute] => Array
                                (
                                    [name] => Array
                                        (
                                        )

                                    [value] => Array
                                        (
                                        )

                                )

                        )

                    [optionPrice] => Array
                        (
                        )

                )

        )

)

I think I'm making a mistake in foreach operations. I can send 1 product as below, I need to send all products in order.

<?php
include "class.n11.php";
$n11Params = [
        'appKey' => 'my-api-key',
        'appSecret' => 'my-api-secret'
];
$n11 = new N11($n11Params);
$xmlfile = file_get_contents("https://tuccar.com.tr/API/n11/100urun.php"); 
$new = simplexml_load_string($xmlfile); 
$con = json_encode($new,JSON_UNESCAPED_UNICODE); 
$newArr = json_decode($con, true); 

foreach ($newArr as &$urun) {

    $productSellerCode = $urun[$i]->productSellerCode;
    $title = $urun[$i]->title;
    $description = $urun[$i]->description;
    $categoryid = $urun[$i]->category->id;
    $price = $urun[$i]->price;
    $preparingDay = $urun[$i]->preparingDay;
    $quantity = $urun[$i]->quantity;
    $saveProduct = $n11->SaveProduct(
                    [
                    'productSellerCode' => $productSellerCode,
                    'title' => $title,
                    'subtitle' => 'Stoktan hızlı kargo',
                    'description' => $description,
                    'category' =>
                    [
                        'id' => $categoryid
                    ],
                    'attributes' =>
                    [
                        'attribute' =>

                            foreach($urun->attributes as $attribute){
                                    'name' => $attribute['name'],
                                    'value' => $attribute['value']
                                    }
                    ],
                    'price' => $price,
                    'domestic' => false,
                    'currencyType' => 'TL',
                    'images' =>
                    [
                        'image' =>

                            foreach($urun->images as $image){
                                    'url' => $image['url'],
                                    'order' => $image['order']
                                    }
                    ],
                    'saleStartDate' => '',
                    'saleEndDate' => '',
                    'productionDate' => '',
                    'expirationDate' => '',
                    'productCondition' => '1',
                    'preparingDay' => $preparingDay,
                    'discount' => '',
                    'shipmentTemplate' => 'kargo-bizden',
                    'stockItems' =>
                    [
                        'stockItem' =>
                        [
                            'quantity' => $quantity,
                            'sellerStockCode' => '',
                            'attributes' =>
                            [
                                'attribute' => []
                            ],
                            'optionPrice' => ''
                        ]
                    ]
                ]
    );

} var_dump($saveProduct);

@RiggsFolly, It worked that way yes but only the first ranked product was sent. I couldn't do the loop.

<?php
 include "class.n11.php";
 $n11Params = ['appKey' => 'my-api-key', 'appSecret' => 'my-api-secret'];
 $n11 = new N11($n11Params);
 $xmlfile = file_get_contents("https://tuccar.com.tr/API/n11/100urun.php"); 
    $new = simplexml_load_string($xmlfile); 
    $con = json_encode($new,JSON_UNESCAPED_UNICODE); 
    $newArr = json_decode($con, true); 
 
 foreach ($newArr['product'] as $product) {
    $saveProduct = $n11->SaveProduct($product);
}
var_dump($saveProduct);

I succeeded with the following code.

var_dump($products);
foreach($products as $indis=>$degeri){
 foreach($degeri as $icindekiler){
  $saveProduct = $n11->SaveProduct($icindekiler);
}
}
var_dump($saveProduct);
erk
  • 1
  • 1
  • 1
  • I think you need to use `array_merge();` `array_merge($array1, $array2);` https://www.php.net/manual/tr/function.array-merge.php –  Dec 31 '19 at 17:32
  • Did you see the missing quote `'` after `'my-api-key,` – RiggsFolly Dec 31 '19 at 17:50
  • If at all possible please try indenting and formatting your code blocks in a way that is a bit easier on the reader. The amount of empty lines is just staggering. – bad_coder Jan 01 '20 at 02:26
  • I fixed it, thank you.'my-api-key', – erk Jan 01 '20 at 10:34

1 Answers1

0

simply start you foreach one step down into the array i.e. from $newArr['product'] and each array you process from there will be the array you want to pass as the parameter.

foreach ($newArr['product'] as $product) {
    $saveProduct = $n11->SaveProduct($product);
}

Also this may or may not actually exist in your real code but this array is not correctly coded, it has a missing '

$n11Params = [
        'appKey' => 'my-api-key',   // missing quote added
        'appSecret' => 'my-api-secret'
];
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149