I'm not sure if anything has changed, but I'm sure, it was possible to pass an array like this:
This request would end up in array (php):
[
'title' => 'Article',
'author' => [
'first_name' => 'John',
'last_name' => 'Doe'
]
]
But if I try send this request now, it is no more possible and sends request like this:
[
'title' => 'Article',
'author[first_name]' => 'John',
'author[last_name]' => 'Doe'
]
What am I doing wrong? How can I achieve sending nested array?
Thanks.
If you pass things like that it will end up as strings on your server. If you are actually trying to pass nested/associative data you can change your body to raw and select JSON as the content-type.
{
"title" : "Article",
"author": {
"first_name": "John",
"last_name" : "Doe"
}
}
Related
I'm trying to set up my own request which I could send to a method for testing purposes, but I can't seem to find how to format it.
Right now I'm trying to pull array out of the request
$data = $request->request->get('itemData');
It should look like this once I get it:
[
'basic' => [
'type' => 1
'country => 1
],
'information' => [
'wadding' => 1
]
]
I have written it like this:
$request->request->set("itemData[basic][type]", 2);
$request->request->set("itemData[information][wadding]", 0);
But I get null since the key is not itemData but for example itemData[basic][type]
How do I format it in the set command so the request parameters are sent in array form?
Figured it out
$request->request->set("itemData",
[
'basic' => ['type' => 2, 'country' => 1],
'information' => ['wadding' => 0]
]
);
We're executing an Elasticsearch query like this using PHP API:
$params = [
//please ignore the variables below,
//we made it in dynamic parameter-based in our function,
//that's why they're variables
'index' => $ourIndex,
'type' => $ourType,
'from' => $from,
'size' => $page_size,
'body' => [
"query" => [
'bool' => [
'must' => [
[
"query_string" => [
"default_field" => $content,
"query" => "$keywords"
]
],
[
"range" => [
"#timestamp" => [
"from" => $parseParams['pub_date_start'],
"to" => $parseParams['pub_date_end'],
'format' => "yy-MMM-dd'T'HH:mm:ss.SSS'Z'",
]
]
]
]
]
]
]
];
The query above works with our #timestamp field because its type is on date
"#timestamp" : {
"type" : "date"
}
And a sample value is like this:
"#timestamp" : "2019-06-17T16:53:55.778Z"
However, we want to target our pub_date field in our index, and in its mapping, the field has a type of long
"pub_date" : {
"type" : "long"
},
so it has this kind of values when we're displaying the documents:
"pub_date" : 1510358400
When we changed the query above to target instead of #timestamp to pub_date, it now displays an error like this:
Tried Solutions
I tried to add an additional format epoch_millis in the format property:
[
"range" => [
"pub_date" => [
"from" => $parseParams['pub_date_start'],
"to" => $parseParams['pub_date_end'],
'format' => "yyyy-MM-dd||yy-MMM-dd'T'HH:mm:ss.SSS'Z'||epoch_millis",
]
]
]
But still fails
Main Question
I feel that the Unix formatted values cant be recognized by the range query of Elasticsearch so that's why the query fails. Is there a work-around for this without changing the MAPPINGS of the index?
Because the other possible solutions suggested to change the mapping, but we already have around 25 million documents in the index, so we thought formatting it in PHP would be a nicer approach
Since the field is of type long and stores the unix timestamp, simply convert the date in $parseParams['pub_date_start'] and $parseParams['pub_date_end'] to unix timestamp using strtotime. Update the range query as below:
"range" => [
"pub_date" => [
"from" => strtotime($parseParams['pub_date_start']),
"to" => strtotime($parseParams['pub_date_end']),
]
]
I'm building and API feed using Element API plugin from Craft, and I'd like the data output to be serialized as a hash, but currently it's returning an array, as example bellow:
<?php
namespace Craft;
return [
'endpoints' => [
'event-name/feed.json' => [
'elementType' => ElementType::Entry,
'criteria' => ['section' => 'event1'],
'transformer' => function(EntryModel $entry) {
$speakersList = [];
foreach ($entry->speakersList as $speaker) {
$speakersList[] = [
'name' => $speaker->speakerFullName,
'jobTitle' => $speaker->speakerJobTitle,
'company' => $speaker->speakerCompany,
];
}
return [
'speakers' => $speakersList,
];
},
],
];
And the output:
{
"data": [
{
"speakers": [
{
"name": "John Doe",
"jobTitle": "Marketing",
"company": "Company 1",
},
...
I've tried the serialize options in the documentation, but none seemed to solve the issue.
Currently if I'd like to access the speakers within data I'd have to first access index[0] to be able to get to the speakers key.
Is there a way to get rid of this array level?
I want to execute multiple queries on elasticsearch server with one request. Specifically I have the following query (is on elastcisearch-php-client)
$params = [
"index" => "bookydate",
"type" => "vendor_service",
"body" => [
"query" => [
"bool" => [
"must" => [
"term" => [
"sys_service_id" => $request->input("sys_service_id")
]
],
"should" => [
"geo_shape" => [
"served_location" => [
"shape" => [
"type" => "point",
"coordinates" => [
"{$request->input('loc_lon')}",
"{$request->input('loc_lat')}"]
]
]
]
]
]
]
]
];
What I want to do is the fetch also all the documents that have the "hole_country" field to true.
What I have tried already is to make another request to Elasticsearch server and with array_merge combine the two results, but did not work because of PHP restrictions on arrays with multiple same keys.
UPDATE
Elastcisearch supports a feature called Multisearch that is exactly what im looking for. The problem is that php-client does not support multisearch so I have to use Guzzle in order to send the requests.
Guzzle docs does not have a full info about how to construct a correct request body. Any info is welcome
Already i have the following body but elastcisearch is returing bad request error
$body = [
["index"=>"bookydate"],
["query"=>["bool"=> ["must"=>[["term"=>["sys_service_id"=>"1"]],["geo_shape"=>["served_location"=>["shape"=>["type"=>"circle","coordinates"=>[25,3],"radius"=>"90km"]]]]]]]],
["index"=>"bookydate"],
["query"=>["bool"=>["must"=>["term"=>["hole_country"=>true]]]]]
];
You can use the multisearch API of Elasticsearch. This is more or less appending all your queries as JSON format in a single POST request. I hope the PHP client supports this, otherwise you might have to manually do the POST request.
Multi-search API
Although it's not documented the Multi Search API is supported by the elasticsearch php client.
Instead of search call msearch, and group your queries like this:
$params = [
'body' => [
["index" => "bookydate", "type" => "vendor_service"],
["query" => [
"bool" => [
"must" => [
"term" => [
"sys_service_id" => $request - > input("sys_service_id")
]
],
"should" => [
"geo_shape" => [
"served_location" => [
"shape" => [
"type" => "point",
"coordinates" => [
"{$request->input('loc_lon')}",
"{$request->input('loc_lat')}"
]
]
]
]
]
]
]]
];
So using your updated syntax is correct. You must just call msearch.
So I'm trying to prepopulate some fields in our DocuSign Templates when submitting them from our custom interface but the request doesn't seem to be able to find them in the templates. We're using REST, cURL and Codeigniter. My data array is as follows:
$data = array("accountId" => $accountId,
"emailSubject" => "Hello World!",
"emailBlurb" => "This comes from PHP",
"templateId" => "********-****-****-****-************",
"templateRoles" => array(
array(
"email" => "****#******.com",
"name" => "**** *****",
"roleName" => "LC3"
),
array(
"email" => $this->input->post("applicant_email"),
"name" => $this->input->post("applicant_name"),
"roleName" => "Applicant",
"tabStatuses" => array(
"textTabs" => array (
array (
"tabLabel" => "lic_num",
"tabValue" => $this->input->post("license_number")
),
array (
"tabLabel" => "ubi_num",
"tabValue" => $this->input->post("ubi_number")
),
array (
"tabLabel" => "tra_nam",
"tabValue" => $this->input->post("trade_name")
)
)
)
)
),
"status" => "sent");
I tried tabs instead of tabStatuses, but that didn't work either and our XML responses have TabStatuses instead of Tabs. Has something changed since the API Walkthroughs were put up?
EDIT: So after much trial and error with Chrome's Postman extension, this is the JSON request that I got to actually not error out:
{
"accountId":"c771ba8c-2947-4bec-acab-15b1b48a11b6",
"emailSubject":"Hello World!",
"emailBlurb":"This comes from PHP",
"templateId":"B96D0480-8792-43E8-AE11-E2AEAC74E601",
"templateRoles":[
{
"email":"mike#cloudpwr.com",
"name":"Mike Longmire",
"roleName":"LC3",
"tabStatuses":[
{
"tabStatus":[
{
"tabLabel":"lic_num",
"tabValue":"1111"
},
{
"tabLabel":"ubi_num",
"tabValue":"2222"
},
{
"tabLabel":"tra_nam",
"tabValue":"Flakey"
}
]
}
],
"email":"duckie715#gmail.com",
"name":"Mike Longmire",
"roleName":"Applicant"
}
],
"status":"sent"
}
I get back my same response:
{
"envelopeId": "0063d398-36b7-4e2f-8515-6ed9ab62aaeb",
"uri": "/envelopes/0063d398-36b7-4e2f-8515-6ed9ab62aaeb",
"statusDateTime": "2013-10-08T18:05:54.9926661Z",
"status": "sent"
}
Any ideas?
This is most likely caused by the values being returned from your function calls inside the JSON not being wrapped by quotation (") marks. To test that theory I would first just hardcode some values wherever you have a function call in your JSON (such as "email" => $this->input->post("applicant_email")) and replace with actual emails, etc and run.
If you still get the 400 error then something else is wrong with your request. If you don't, then you just need to prepend and append quotes around the values that are passed back by those function calls.
For instance, you can assign to variables, such as
$applicantEmail_1 = $this->input->post("applicant_email")
then setup your JSON like:
"templateRoles" => array(
array(
"email" => "****#******.com",
"name" => "**** *****",
"roleName" => "LC3"
),
array(
"email" => "$applicantEmail_1",
"name" => $this->input->post("applicant_name"),
"roleName" => "Applicant",
...
The nice thing about PHP is that even though that variable is in double quotes the value of the variable will still be inserted inside the quotes.