Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Problems working with API (GET, POST method with PHP and Wordpress)

I am using wp_remote_post() to post products to a third party site. First I get these products via API from a site called Mercado Libre. But when I carry out the mass publication, the products are published without a name, without a price, and without respecting the parameters provided. They only have the name "Product".

function publish_products_in_woocommerce()
{

    $respuesta = wp_remote_get('https://api.mercadolibre.com/sites/MLA/search?q=Motorola%20G6', array(
        'headers' => array(
            'Authorization' => 'Bearer APP_USR-2778915669536100-052221-0f3a39361bdcea9bc3de0df8ab619f66-370993848'
        )
    ));
    $respuesta = json_decode($respuesta['body'], true);
    print_r($respuesta);
    foreach ($respuesta['results'] as $value) {
        $product_data = array(
            'name' => $value['title'],
            'status' => 'draft',
            'type' => 'simple',
            'regular_price' => $value['price'],
            'description' => $value['title'],
            'short_description' => $value['title'],
            'categories' => [
                [
                    'id' => $value['category_id'],
                ]
            ],
            'images' => [
                [
                    'src' => $value['thumbnail']
                ]
            ]

        );

        $woocommerce_api_ck = 'ck_b2a0f58d07590e8283302eca04fbc1b66a9ff653';
        $woocommerce_api_cs = 'cs_b4d91662590be47416f663fc9f0d1c49f600e394';

        $url = 'http://nuevo.labisbal.com.ar/wp-json/wc/v3/products';

        wp_remote_post('http://nuevo.labisbal.com.ar/wp-json/wc/v3/products', array(
            'headers' => array(

                'Authorization ' => 'Basic ' . base64_encode($woocommerce_api_ck . ':' . $woocommerce_api_cs),
            ),

            'body' =>  $ProductToWooCommerce = json_encode($product_data),
            'method' => 'POST',
            'timeout' => 145,
            'blocking' => false,
            'sslverify' => false,
            'stream' => true,
            'data_format' => 'json'




        ));
        print_r($ProductToWooCommerce);
        if (wp_remote_retrieve_response_message($respuesta) === 'Created') {
            echo 'The product  has been created';
        }
    }
}
publish_products_in_woocommerce();

?>

It's strange because when I display the json already converted (using json_encode) the data is displayed correctly... (using print_r)

wordpress_api_development

wordpress_api_development