如何将 PHP 多维关联数组转换为 API 格式为 API 的 http 查询?

How to convert PHP multidimensional associative array to API http query in the format the API specifies?

我有以下数组

$folder_data = array(
    "title" => "Testing API Creation",
    "description" => "Testing the Wrike API by creating this folder.",
    "project" => array(
        "status" => "OnHold",
        "startDate" => "2022-05-19",
        "endDate" => "2022-06-19",
        "contractType" => "Billable",
        "budget" => 100
    )
);

当我通过 http_build_query() 运行 它时,它给了我这个(为清楚起见使用 urldecode()):

title=Testing API Creation&description=Testing the Wrike API by creating this folder.&project[status]=OnHold&project[startDate]=2022-05-19&project[endDate]=2022-06-19&project[contractType]=Billable&project[budget]=100

API 抛出一个错误,指出项目[status] 是一个无效参数 API 文档在 curl 示例中给出了这一点。您可以看到“project”的数据分组:

title=Test folder&description=Test description&project={"ownerIds":["KUFK5PMF"],"startDate":"2021-10-19","endDate":"2021-10-26","contractType":"Billable","budget":100}

他们正在将查询嵌套到对象中,我猜?我将如何使用我的 PHP 数组来做到这一点? 我尝试了一个有人做过的递归函数,但它也没有给我想要的东西。

感谢任何帮助!谢谢!

project 参数在他们的示例中是 JSON,因此请使用 json_encode() 来创建它。

$folder_data = array(
    "title" => "Testing API Creation",
    "description" => "Testing the Wrike API by creating this folder.",
    "project" => json_encode(array(
        "status" => "OnHold",
        "startDate" => "2022-05-19",
        "endDate" => "2022-06-19",
        "contractType" => "Billable",
        "budget" => 100
    ))
);