admin管理员组

文章数量:1122832

I do not know how to properly encode this multidimensional JSON record to query an online collection of records:

{
    "conditions" : [
    {
     "fullpath" : {
        "operator" : "starts_with",
        "value" : "/Destination Structure Test/2-Thinking Outside the Borders. Library Leadership in a World Community/"
        }
    },
    {
        "filename" : {
            "operator" : "eq",
            "value" : "ThinkingOutsideTheBorders.pdf"
        }
    }   
    ]
}

to be able to POST it. I am getting a 404 error, which I am assuming means that I am successfully authenticating properly but am not able to pass on these search parameters. I have looked at other code examples but I simply am missing a major concept, apparently.

My attempt at modelling it in PHP:

    <?php

    $apiUrl = 'https://the_url_I_am_using';

    $bearerToken = 'wombats';

    $ch = curl_init($apiUrl);

$data = array();
$data["conditions"] = array();
$conditions["fullpath"] = array();
$conditions["filename"] = array();

$fp = array();
$fp["operator"] = "starts_with";
$fp["value"] = "/Destination Structure Test/2-Thinking Outside the Borders/";
$conditions["fullpath"][] = $fp;

$fn = array();
$fn["operator"] = "eq";
$fn["value"] = "ThinkingOutsideTheBorders.pdf";
$conditions["filename"][] = $fn;

$data = http_build_query($data);
$payload = json_encode(array("user" => $data));

curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  // Return the response as a string
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Authorization: Bearer $bearerToken",  // Set the Authorization header
    "Content-Type: application/json",      // Specify that we are sending/receiving JSON
]);

$response = curl_exec($ch);

if(curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);  // If there's an error, print it
} else {
    // Print the response
    echo 'Response: ' . $response;
}

// Close the cURL session
curl_close($ch);

I do not know how to properly encode this multidimensional JSON record to query an online collection of records:

{
    "conditions" : [
    {
     "fullpath" : {
        "operator" : "starts_with",
        "value" : "/Destination Structure Test/2-Thinking Outside the Borders. Library Leadership in a World Community/"
        }
    },
    {
        "filename" : {
            "operator" : "eq",
            "value" : "ThinkingOutsideTheBorders.pdf"
        }
    }   
    ]
}

to be able to POST it. I am getting a 404 error, which I am assuming means that I am successfully authenticating properly but am not able to pass on these search parameters. I have looked at other code examples but I simply am missing a major concept, apparently.

My attempt at modelling it in PHP:

    <?php

    $apiUrl = 'https://the_url_I_am_using';

    $bearerToken = 'wombats';

    $ch = curl_init($apiUrl);

$data = array();
$data["conditions"] = array();
$conditions["fullpath"] = array();
$conditions["filename"] = array();

$fp = array();
$fp["operator"] = "starts_with";
$fp["value"] = "/Destination Structure Test/2-Thinking Outside the Borders/";
$conditions["fullpath"][] = $fp;

$fn = array();
$fn["operator"] = "eq";
$fn["value"] = "ThinkingOutsideTheBorders.pdf";
$conditions["filename"][] = $fn;

$data = http_build_query($data);
$payload = json_encode(array("user" => $data));

curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  // Return the response as a string
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Authorization: Bearer $bearerToken",  // Set the Authorization header
    "Content-Type: application/json",      // Specify that we are sending/receiving JSON
]);

$response = curl_exec($ch);

if(curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);  // If there's an error, print it
} else {
    // Print the response
    echo 'Response: ' . $response;
}

// Close the cURL session
curl_close($ch);
Share Improve this question edited Nov 22, 2024 at 23:22 Sammitch 32.1k7 gold badges56 silver badges90 bronze badges Recognized by PHP Collective asked Nov 22, 2024 at 23:21 Andrew BullenAndrew Bullen 11 silver badge
Add a comment  | 

1 Answer 1

Reset to default 1

You already know what the structure should be, just change it from {"key": "value"} JSON syntax to ["key" => "value"] PHP syntax.

$data = [
    "conditions" => [
        [
            "fullpath" => [
                "operator" => "starts_with",
                "value" => "/Destination Structure Test/2-Thinking Outside the Borders. Library Leadership in a World Community/"
            ]
        ],
        [
            "filename" => [
                "operator" => "eq",
                "value" => "ThinkingOutsideTheBorders.pdf"
            ]
        ]   
    ]
]

And if you're sending this as JSON, you shouldn't call http_build_query() first. Get rid of

$data = http_build_query($data);

本文标签: POSTing multidimensional JSON parameters through curl in PHPStack Overflow