I am sending request using curl.
this is json data :
{
"users": [
{
"userId": "123",
"userLink" : "www.example .com",
"userType": "au"
}
]
}
I am trying this but i need same above json:
$post = array(
"users" => array(
"userId" => "123",
"userLink" => "link",
"userType" => "au"
)
);
$out = array_values($post);
$post = json_encode($out);
print_r($post);
You need an additional array dimension, and if you use brackets [ ] you can easily see how they look the same:
$post = [
"users" => [
[
"userId" => "123",
"userLink" => "link",
"userType" => "au"
]
]
];
$post = json_encode($post);
Also, don't use array_values; that will remove the string key users.
Related
I'm trying to send this data in a request:
{
"TABLENAME":"USERS",
"DATA": [
{
"ID": "1",
"NAME":"JOHN",
"AGE":"33",
"GENDER":"M",
"DATE":"2000000",
"CITY":"LONDON"
},
{
"ID": "2",
"NAME":"MARK",
"AGE":"35",
"GENDER":"M",
"DATE":"2000000",
"CITY":"MUNICH"
}
]
}
I'm retrieving all the hook data and storing it in my hook handler like:
$data = $this->webhookCall;
And access the payload through:
$payload = $data->payload;
The $payload first of all is an array and the key values are in reverse order, and looks like this:
array (
'DATA' =>
array (
0 =>
array (
'ID' => '1',
'AGE' => '33',
'CITY' => 'LONDON',
'DATE' => '2000000',
'NAME' => 'JOHN',
'GENDER' => 'M',
),
1 =>
array (
'ID' => '2',
'AGE' => '35',
'CITY' => 'MUNICH',
'DATE' => '2000000',
'NAME' => 'MARK',
'GENDER' => 'M',
),
),
'TABNAME' => 'USERS',
)
Also when trying to save to db:
$hook = new USER;
$hook->name = $data->name;
$hook->url = $data->url;
$hook->headers = json_encode($data->headers);
$hook->payload = json_encode($data->payload);
$hook->exception = $data->exception;
$hook->save();
The data is still with the wrong key order:
{"DATA": [{"ID": "1", "AGE": "33", "CITY": "LONDON", "DATE": "2000000", "NAME": "JOHN", "GENDER": "M"}, {"ID": "2", "AGE": "35", "CITY": "MUNICH", "DATE": "2000000", "NAME": "MARK", "GENDER": "M"}], "TABNAME": "USERS"}
I have tried to reverse the $payload array:
$hook->payload = json_encode(array_reverse($data->payload));
but the data is still in the wrong order in the db.
Can someone please tell me what is wrong here and why this behavior?
And also why does an object sent in a request is being converted to an array? Is this normal behavior for php or specific for this package that I'm using?
Thanks
I am trying to change an indexed array of arrays into a new array structure. My data is as follows:
$arr = array(
array( "year" => 1921, "name" => "bob" ),
array( "year" => 1944, "name" => "steve" ),
array( "year" => 1944, "name" => "doug" ),
array( "year" => 1921, "name" => "jim" ),
);
I would like to recreate a new array thats groups them into the same year. The best I can come up with is designating the year as the key so any row that has that year gets added to that key, but it's not the output that I'm looking for. What I need is how it's listed below.
"data": [
{
"year":"1921",
"names": [
{ "name":"bob" }, { "name":"jim"}
]
},
{
"year":1944",
"names": [
{ "name":"steve" }, { "name":"doug "}
]
}
You don't need to pre-extract the unique years nor use conditions within nested loops. Just push the data into the result set using temporary first-level keys, then remove the temporary keys when finished looping.
This ensures unique years, but allows names to be duplicated.
Code: (Demo)
$arr = [
["year" => 1921, "name" => "bob"],
["year" => 1944, "name" => "steve"],
["year" => 1944, "name" => "doug"],
["year" => 1921, "name" => "jim"],
];
foreach ($arr as $item) {
$result[$item['year']]['year'] = $item['year'];
$result[$item['year']]['names'][] = ['name' => $item['name']];
}
echo json_encode(
['data' => array_values($result)],
JSON_PRETTY_PRINT // for better visualization
);
Output:
{
"data": [
{
"year": 1921,
"names": [
{
"name": "bob"
},
{
"name": "jim"
}
]
},
{
"year": 1944,
"names": [
{
"name": "steve"
},
{
"name": "doug"
}
]
}
]
}
I am trying to create a POST to a REST API to create a new object. I cannot figure out how to properly format my JSON.
Here's the response from the GET of an existing object:
{
"name": "product 2 mem"
"type": "simple"
"categories": array:1 [▼
0 => {
"id": 75
}
]
"meta_data": array:1 [▼
"id": 3665
"key": "_yith_wcbm_product_meta"
"value": {
"id_badge": "2955"
}
}
]
}
Here is the POST I'm trying to create:
$data = [
'name' => 'product name',
'type' => 'simple',
'categories' => [
[
'id' => 75
],
'meta_data' => [
'_yith_wcbm_product_meta' => [
'id_badge' => '2955'
]
]
];
You got typo in you json data.
$response = '{
"id": 3665,
"key": "_yith_wcbm_product_meta",
"value": {
"id_badge": "2955"
}
}';
$array = json_decode($response,true);
$return = ['meta_data'=>['key'=>$array['key'],'value'=>$array['value']]];
echo json_encode($return);
I figured out how to format it:
'meta_data' => [
[
'key' => '_yith_wcbm_product_meta',
'value' => ['id_badge' => '2955']
]
I add a new contact API - send POST, according to the example below.
https://intranet_name.bitrix24.com/rest/crm.contact.add?auth=authentication_code&fields[NAME]=Maria&fields[SECOND_NAME]=Anna&fields[LAST_NAME]=Nowacka
It adds correctly, works.
I can not deal with the addition of the PHONE array, how to do it?
<script type="text/javascript">
BX24.callMethod(
"crm.contact.add",
{
fields:
{
"NAME": "John",
"SECOND_NAME": "Lancelot",
"LAST_NAME": "Doe",
"OPENED": "Y",
"ASSIGNED_BY_ID": 1,
"TYPE_ID": "CLIENT",
"SOURCE_ID": "SELF",
"PHOTO": { "fileData": document.getElementById('photo') },
"PHONE": [ { "VALUE": "555888", "VALUE_TYPE": "WORK" } ]
},
params: { "REGISTER_SONET_EVENT": "Y" }
},
function(result)
{
if(result.error())
console.error(result.error());
else
console.info("Created a new contact; ID=" + result.data());
}
);
OAuth 2.0 Protocol API documentation
I changed to uURL, it works.
You had to add a square bracket.
$data = array(
"fields" => array(
"NAME" => "Nowy2",
"LAST_NAME" => "Testowy1",
"ADDRESS" => "Nowodąbrowska 45",
"ADDRESS_POSTAL_CODE" => "54-345",
"ADDRESS_CITY" => "Warszawa",
"ADDRESS_COUNTRY" => "Polska",
"TYPE_ID" => "CLIENT",
"PHONE" => array([
"VALUE" => 994556765,
"VALUE_TYPE" => "WORK"]
)
)
);
I'm working with Elasticsearch-PHP client. I want to index my datas, but I have a problem with mapping. There is a problem: I create my data array, everything works fine, but when I add this array to my index body => my_data_array some datas show up, but not all of them. I don't know why. I just digged and try all of following steps but nothing changed.
I just attached my snippets.
This my controller file where I index datas:
{
$params = ['body' => []];
foreach($all_ads as $key => $ads){
$params['body'][] = [
'index' => [
'_index' => 'demo_data',
'_type' => 'demo',
'_id' => $ads->id
]
];
$params['body'][] = $ads->indexParams();
}
$responses = $client->bulk($params);
this is result json object:
"response": [
{
"id": 85345,
"old_id": "5088063",
"user_id": "2706",
"category_id": "15",
"type": "3",
"title": array[3],
"slug": "",
"sub_region_id": "8",
"condition": "1",
"username": "John Doe",
"price": "82000",
"price_type": "1",
"no_phone": "0",
"views": "29",
"hot": "0",
"vip": "0",
"price_measure": "0"
}
I have a datafield and it is not visible here.
Its data mapping structure
'data' => [
'type' => 'object',
'properties' => [
'key_id' => ['type' => 'integer'],
'value_id' => ['type' => 'integer'],
'key' => ['type' => 'keyword'],
'value' => ['type' => 'keyword']
]
]
How can I fix it?
I just check some question from here and github issue forum but nothing helps. Thanks!
You have to follow solution of the question. Its maybe helpful for you.
Elasticsearch mapping not working as expected
Ask Question