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
1 Answer
Reset to default 1You 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
版权声明:本文标题:POSTing multidimensional JSON parameters through curl in PHP - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736300377a1930793.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论