E
E
Eugene2018-10-23 10:42:46
JSON
Eugene, 2018-10-23 10:42:46

How to correctly collect json for sending via API?

Good afternoon!
It is necessary to send via wp_remote_request () to send json of the form,
5bcecf8b00215447801646.png
I collect it like this

for ($i = 1; $i <= 5; $i++) {
                $alt_url = get_post_meta($post_id, 'alt_page_'.$i, true);
                if ($alt_url) {
                    // TODO: добавить массив
                    $alternative_pages_array[] = array(
                        array(
              'id'     => $alt_url,
                        	'active' => true
            ),
                    );
                }
            }

            $current_url = get_permalink($post_id);
      $new_array = array(
                'base_id'   => $current_url,
        'urls'      => $alternative_pages_array
      );
$args_urls = array(
        'method'	=> 'POST',
                'timeout'     => 45,
                'redirection' => 5,
                'headers' => array(
                    'content-type' =>  'application/json',
                    'x-access-key' =>  $access_key,
                ),
        'body' => json_encode($new_array, JSON_UNESCAPED_SLASHES),
            );

at the output I get the following array with extra brackets, what am I doing wrong?
"base_id":"http://site.ru/base/",
        "urls":[
            [{
                "id":"http://site.ru/page1-2",
                "active":true
            }],
            [{
                "id":"http://site.ru/page2-2",
                "active":true
            }],
            [{
                "id":"http://site.ru/page3-2",
                "active":true
            }],
            [{
                "id":"http://site.ru/page4-2",
                "active":true
            }],
            [{
                "id":"http://site.ru/page5-2",
                "active":true
            }]
        ]
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Antony Tkachenko, 2018-10-23
@redmo

$alternative_pages_array[] = array(
              'id'     => $alt_url,
              'active' => true
);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question