API Product List String to PHP Array [closed]

I am attempting to grab specific products from one site and pass them to another website using WooCommerce Rest API. I have the Rest API working and it grabs the desired products, however I’m getting stuck at the response. Here are the steps I’m working with:

  1. Create a cURL request to grab the products from the Parent Site
  2. Use the response from the cURL request to loop through each product
  3. Inside of the loop update the fields returned from the response.

Here is my PHP code:

    function test_api() {
    $url = "https://www.example.com/wp-json/wc/v3/products";

    $consumer_key = 'XXX';
    $consumer_secret = 'XXX';
    
    $params = array(
        'consumer_key'      => $consumer_key,
        'consumer_secret'   => $consumer_secret,
        'tag'               => 5303,
        '_fields'           => 'id,name,regular_price,sku,permalink,short_description,categories,stock_status,images,type,variations,status',
    );
    
    $endpoint = $url.'?'.implode('&', array_map(
        function($v, $k) {
            return sprintf("%s=%s", $k, $v);
        }, $params, array_keys($params)
    ));

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $endpoint);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_USERPWD, "$consumer_key:$consumer_secret");
    
    $result = curl_exec ($ch);
    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close ($ch);
    
    //Continue the functions
    parse_api_products(json_decode($result, true));
    
    wp_send_json_success(json_decode($result, true)); //Using this as ajax for testing for now
} 

function parse_api_products($response) {
    
    $product_list = print_r($response, true);
    $date_updated = date('m-d-Y g:ia');
    
    //Log the response
    $logger = wc_get_logger();
    $context = array('source' => 'api-product-sync');
    $logger->info('API Product Sync: ' . $product_list, $context);
    
    $logger->warning($product_list, array('source' => 'test'));
    
    foreach ($product_list as $item) {
        $sku = $item->sku;
        $product_id = wc_get_product_id_by_sku( $sku );
        
        if($product_id) {
            $product = wc_get_product($product_id);
        
            wp_update_post([
                'ID' => $product_id,
                'post_title' => $item->name
            ]);

            update_post_meta( $product_id, '_regular_price',  $item->stock_status);
            update_post_meta( $product_id, '_stock_status',  $item->stock_status);
            $product->set_short_description($item->short_description);
        }
    }
}

The error code I end up with is:

[proxy_fcgi:error [pid 1752176:tid 1752345 [client 69.92.178.86:0 AH01071: Got error 'PHP message: PHP Warning: foreach() argument must be of type array|object, string given in /home/13622.cloudwaysapps.com/wjzzcseneb/public_html/wp-content/themes/bricks-child/functions.php on line 602', referer: https://woocommerce-1362296-50153.cloudwaysapps.com/test-page/

I also added this data to the WC Logger and get the following result:

2024-12-12T20:19:18+00:00 Warning Array
(
    [0] => Array
        (
            [id] => 205881
            [name] => AA Test Product
            [regular_price] => 19
            [sku] => skutestaa01
            [permalink] => https://www.example.com/?post_type=product&p=205881
            [short_description] => 
            [categories] => Array
                (
                    [0] => Array
                        (
                            [id] => 817
                            [name] => Phone Cases
                            [slug] => phone-cases
                        )

                    [1] => Array
                        (
                            [id] => 870
                            [name] => Necklaces
                            [slug] => necklaces
                        )

                )

            [images] => Array
                (
                    [0] => Array
                        (
                            [id] => 206076
                            [date_created] => 2024-12-04T06:19:06
                            [date_created_gmt] => 2024-12-04T20:19:06
                            [date_modified] => 2024-12-04T07:09:43
                            [date_modified_gmt] => 2024-12-04T21:09:43
                            [src] => https://cdn.example.com/2024/12/Winter.jpg
                            [name] => Winter
                            [alt] => 
                        )

                    [1] => Array
                        (
                            [id] => 206075
                            [date_created] => 2024-12-04T06:19:00
                            [date_created_gmt] => 2024-12-04T20:19:00
                            [date_modified] => 2024-12-04T07:28:37
                            [date_modified_gmt] => 2024-12-04T21:28:37
                            [src] => https://cdn.example.com/2024/12/New-Brands.jpg
                            [name] => New Brands
                            [alt] => 
                        )

                )

            [type] => simple
            [variations] => Array
                (
                )

            [status] => draft
            [stock_status] => instock
        )

    [1] => Array
        (
            [id] => 206510
            [name] => AA Test Product #2
            [regular_price] => 
            [sku] => aa-sku-test-0
            [permalink] => https://www.example.com/?post_type=product&p=206510
            [short_description] => <p>This is a test!</p>

            [categories] => Array
                (
                    [0] => Array
                        (
                            [id] => 2038
                            [name] => Top Sellers
                            [slug] => top-sellers
                        )

                )

            [images] => Array
                (
                    [0] => Array
                        (
                            [id] => 206488
                            [date_created] => 2024-12-12T01:06:46
                            [date_created_gmt] => 2024-12-12T15:06:46
                            [date_modified] => 2024-12-12T01:06:46
                            [date_modified_gmt] => 2024-12-12T15:06:46
                            [src] => https://cdn.example.com/2021/05/ST.jpg
                            [name] => ST
                            [alt] => 
                        )

                )

            [type] => variable
            [variations] => Array
                (
                    [0] => 206511
                    [1] => 206512
                )

            [status] => draft
            [stock_status] => instock
        )

)

The current $product_list variable matches a PHP array but for some reason is returning as a string causing the error. I’ve tried using the JSON response from the initial cURL response but it also throws various errors as it returns as a string (even when I attempt to decode it). What can I do to get this variable to read as an array so I can process the updates in the foreach loop?

6

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật