格式化对象数组以获取 Postman/GET 请求可能的 json 输出

发布于 2025-01-13 12:39:12 字数 1838 浏览 0 评论 0原文

所以我似乎无法弄清楚这一点,所以我正在伸出援手,看看是否有人能够帮助我。

请让我知道最佳输出是什么,以便我可以使用 GET 为我创建的端点检索干净的数据

我有以下方法

function instagram_posts(): bool|string
{
    if (!function_exists('is_plugin_active')) {
        include_once(ABSPATH . 'wp-admin/includes/plugin.php');
    }
    if (!is_plugin_active('fh-instagram/autoload.php')) {
        return false;
    }
    if (empty($items = Instagram::get_items_for_api(50))) {
        return false;
    }
    var_dump($items);

    var_dump(json_encode($items));

    return json_encode($items);
}

var_dump($items); 给出以下输出:

array(50) {
  [0]=>
  object(Plugin\Instagram\Item)#976 (7) {
    ["id":"Plugin\Instagram\Item":private]=>
  }
  [1]=>
  object(Plugin\Instagram\Item)#1030 (7) {
    ["id":"Plugin\Instagram\Item":private]=>
    string(17) "17842233125750202"
  }
}

当我运行 var_dump(json_encode($items)); 我得到以下输出:

string(151) "[{},{}]"

如何转换对象数组,以便将其转换为 json,然后在 Postman 中使用它?这就是它目前在 Postman 中的样子:

array(50) {
  [0]=>
  object(Plugin\Instagram\Item)#973 (7) {
    ["id":"Plugin\Instagram\Item":private]=>
    string(17) "17992874035441353"
  }
  [1]=>
  object(Plugin\Instagram\Item)#1027 (7) {
    ["id":"Plugin\Instagram\Item":private]=>
    string(17) "17842233125750202"
  }
}

它应该输出,例如:

[
  {"id": etc..}
]

所有帮助将不胜感激!

下面使用 instagram_posts 方法:

add_action('rest_api_init', function () {
    register_rest_route( 'instagram', '/posts/', [
        'methods'  => 'GET',
        'callback' => 'instagram_posts',
    ]);
});

所以我可以使用 Postman 访问端点:http://headlesscms.com.local/wp-json/instagram/posts

So I can't seem to figure this out, so I'm reaching out to see if someone might be able to help me.

Please let me know what the best output is so that I could use GET to retrieve clean data for the endpoint that I've created.

I have the following method:

function instagram_posts(): bool|string
{
    if (!function_exists('is_plugin_active')) {
        include_once(ABSPATH . 'wp-admin/includes/plugin.php');
    }
    if (!is_plugin_active('fh-instagram/autoload.php')) {
        return false;
    }
    if (empty($items = Instagram::get_items_for_api(50))) {
        return false;
    }
    var_dump($items);

    var_dump(json_encode($items));

    return json_encode($items);
}

var_dump($items); gives me the following output:

array(50) {
  [0]=>
  object(Plugin\Instagram\Item)#976 (7) {
    ["id":"Plugin\Instagram\Item":private]=>
  }
  [1]=>
  object(Plugin\Instagram\Item)#1030 (7) {
    ["id":"Plugin\Instagram\Item":private]=>
    string(17) "17842233125750202"
  }
}

When I run var_dump(json_encode($items)); I get the following output:

string(151) "[{},{}]"

How can I convert my array of objects so that it can transform it to json and then use it within Postman? This is what it currently looks like in Postman:

array(50) {
  [0]=>
  object(Plugin\Instagram\Item)#973 (7) {
    ["id":"Plugin\Instagram\Item":private]=>
    string(17) "17992874035441353"
  }
  [1]=>
  object(Plugin\Instagram\Item)#1027 (7) {
    ["id":"Plugin\Instagram\Item":private]=>
    string(17) "17842233125750202"
  }
}

It should be outputted such as:

[
  {"id": etc..}
]

All help will be appreciated!

The instagram_posts method is being use below:

add_action('rest_api_init', function () {
    register_rest_route( 'instagram', '/posts/', [
        'methods'  => 'GET',
        'callback' => 'instagram_posts',
    ]);
});

So I can use Postman to access the endpoint: http://headlesscms.com.local/wp-json/instagram/posts

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迎风吟唱 2025-01-20 12:39:12

由于您想要的属性是私有的,因此它不会包含在 json_encode() 的结果中。只有公共财产才会。

您需要创建一个具有所需结构的多维数组并对其进行编码。

// This is the new array we will push the sub arrays into
$results = [];

foreach($items as $item) {
    $results[] = ['id' => $item->get_id()];
}

return json_encode($results);

这将为您提供如下所示的 json 结构:

[
    {
        "id": 1
    },
    {
        "id": 2
    },
    ...
]

替代格式

如果您只需要 id 列表,则可能不需要创建多维数组,而只需返回 id 列表。

在这种情况下,请执行以下操作:

$results = [];

foreach ($items as $item) {
    $results[] = $item->get_id();
}

return json_encode($results);

这将为您提供:

[
    1,
    2,
    ...
]

Since the property you want is private, it won't be included in the results of json_encode(). Only public properties will.

You need to create a multidimensional array with the structure you want and encode that.

// This is the new array we will push the sub arrays into
$results = [];

foreach($items as $item) {
    $results[] = ['id' => $item->get_id()];
}

return json_encode($results);

This will give you a json structure that looks like:

[
    {
        "id": 1
    },
    {
        "id": 2
    },
    ...
]

Alternative format

If you only want a list of id's, you might not need to create a multidimensional array, but rather just return a list of ids.

In that case, do this:

$results = [];

foreach ($items as $item) {
    $results[] = $item->get_id();
}

return json_encode($results);

That would give you:

[
    1,
    2,
    ...
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文