WP_remote_post 如何向 JSON API 调用添加过滤器

问题描述 投票:0回答:2

我正在尝试将 API 集成到我的 Wordpress 插件中。以下 PHP 代码成功连接到 API 并检索房地产列表(API 来自房地产软件):

$url = 'https://api.whise.eu/v1/estates/list';

$body = array(
    'Filter' => array( 'languageId' => 'nl-BE'),
);

$args = array(
    'headers' => array( 'Authorization' => 'Bearer ' . $token),
    'body' => json_decode($body)
);

$response = wp_remote_post($url,$args);

根据文档(http://api.wise.eu/WebsiteDesigner.html#operation/Estates_GetEstates)可以过滤结果,但我无法让它工作。我对 API 和 JSON 没有太多经验,所以我可能会在这里遗漏一些东西。

即使我按照文档中的说明添加了语言过滤器,上面的代码仍然会检索英语数据。当我将

'body' => json_decode($body)
替换为
'body' => $body
时,我得到以下响应:

{"Message":"The request entity's media type 'application/x-www-form-urlencoded' is not supported for this resource."}

谢谢!

json wordpress api
2个回答
3
投票

只是为了这个问题不会得不到答案:

  • 您需要添加
    Content-Type
    标题并将其设置为
    application/json
    。这样端点就可以将您的数据解释为 JSON 字符串。
  • 您还需要将
    'body' => json_decode($body)
    更改为
    'body' => json_encode($body)
    ,因为您要将
    $body
    数组转换为 JSON 字符串(请参阅 json_decode()json_encode())。

这就是您的代码现在的样子:

$url = 'https://api.whise.eu/v1/estates/list';

$body = array(
    'Filter' => array( 'languageId' => 'nl-BE'),
);

$args = array(
    'headers' => array(
        'Authorization' => 'Bearer ' . $token,
        'Content-Type' => 'application/json'
    ),
    'body' => json_encode($body)
);

$response = wp_remote_post($url,$args);

0
投票

你能告诉我如何找到 'Authorization' => 'Bearer ' 吗? $代币,

© www.soinside.com 2019 - 2024. All rights reserved.