0

I have the following array

$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
    )
);

When I run it through http_build_query(), it gives me this (urldecode() used for clarity):

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

The API throws an error saying that project[status] is an invalid parameter The API docs give this within the curl example. You can see that the data for "project" is grouped:

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

They're nesting the query into objects, I guess? How would I go about doing that with my PHP array? I tried a recursive function someone had done, but it didn't give me what it's looking for either.

Any help is appreciated! Thanks!

Mattaton
  • 247
  • 2
  • 10

1 Answers1

2

The project parameter is JSON in their example, so use json_encode() to create that.

$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
    ))
);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Gotcha. The API docs called the 'project' portion an object, so I followed that terminology. They're also using arrays for other portions of the query string in this format: metadata=[{"key":"testMetaKey","value":"testMetaValue"}]. This data will be pulled from another API, so I won't know exactly how the php array will be structured. – Mattaton May 13 '22 at 22:30
  • Here's the full query string example they give. It has objects, arrays, and objects nested in arrays. Egads! `'metadata=[{"key":"testMetaKey","value":"testMetaValue"}]&customFields=[{"id":"IEC7PGVEJUARLTHD","value":"testValue"}]&description=Test description&project={"ownerIds":["KUFK5PMF"],"startDate":"2021-10-19","endDate":"2021-10-26","contractType":"Billable","budget":100}&title=Test folder&shareds=["KUFK5PMF"]'` – Mattaton May 13 '22 at 22:32
  • 1
    Do the same thing for all the parameters that are nested objects or arrays. – Barmar May 13 '22 at 22:35
  • I built a little function to handle the query structure. API is telling me I have insufficient user rights, but the query structure is good. Thanks! – Mattaton May 13 '22 at 22:57