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!

0

1 Answer 1

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
    ))
);
Sign up to request clarification or add additional context in comments.

4 Comments

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.
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"]'
Do the same thing for all the parameters that are nested objects or arrays.
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!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.