Need help to update values in ACF via WP API - php

I'm trying to update an item particularly with the "rows" feature in WordPress ACF plugin via PHP using Wordpress API.
The value I'm looking to change is "Monday"
Here's the json from the endpoint:
{
"id": 765,
"acf": {
"contact": "2150",
"sched": [
{
"field_611218a978d03": "Monday",
"field_6112197d78d04": "00:00:00",
"field_6112199f78d05": "17:00:00"
}
],
},
}
I can change the value of "2150" to "2151" with the following:
'body' => array(
'acf' => array (
'contact' => "2151",
),
)
But I can't change the value of "Monday", I've already tried the following:
'body' => array(
'acf' => array (
'sched' => array ( array ( "field_611218a978d03" => "Tuesday")
)
)
As well as:
'body' => array(
'acf' => array (
'sched' => array (
[0] => array( "field_611218a978d03" => "Tuesday")
)
)
)
I've been searching and there seems to be no answer pertaining to this specific problem. I've been stumped for days now. Any help would sincerely be appreciated!
Edit:
To answer CBroe this is the code I'm working with:
$api_response = wp_remote_post( 'domain.com/wp-json/wp/v2/person/765', array(
'headers' => array('Authorization' => 'Basic ' . base64_encode( 'user:pass' )),
'body' => array(
'acf' => array (
'contact' => '2150',
'sched' => array ( array ( "field_611218a978d03" => "Tuesday")
),
)
));
$body = json_decode( $api_response['body'],true);

Related

Posting data using Guzzle

I am trying to post data using Guzzle 6,
The request which I am generating dynamically looks as below:
$postRequest = Array(
'headers' => array(
['x-api-key'] => 'srDxd39M2FQxxvfvxxcIohcLfKDcdcRUU'
)
'form_params' => array(
[0] => Array (
['name'] => 'function_key'
['contents'] => 'REGISTER'
)
[1] => Array (
['name'] => 'email'
['contents'] => 'tester#test.com'
)
[2] => Array (
['name'] => 'password'
['contents'] => 'test'
)
[3] => Array (
['name'] => 'name'
['contents'] => 'tester'
)
[4] => Array (
['name'] => 'is_org'
['contents'] => 'N'
)
)
)
// Sending Request using 'POST' Method
$client = new GuzzleHttp\Client();
$response = $client->request('POST','abcdxyz.com',$getRequest);
Now my problem is, The Response which I am receiving after sending the above request says "API 'function_key' is missing".
But I am sending 'function_key' as part of request, then What I am missing here ?? Why it is not able to find 'function_key'
Any help is Appreciated
TIA
Try formating it as follows:
$postRequest = [
'headers' => [
'x-api-key' => 'srDxd39M2FQxxvfvxxcIohcLfKDcdcRUU'
],
'form_params' => [
'function_key' => 'REGISTER',
'email' => 'tester#test.com',
'password' => 'test',
'name' => 'tester',
'is_org' => 'N'
]
]

elasticsearch bool query error in php client

ElasticSearch returns me [_na] query malformed, no field after start_object error when trying to look up entries using the following query. The field localtime is a new field of documents and exist in every document.
php code,
$qryurl = '<myurl>:<myport>/index/_search?pretty';
$data = array(
"query" => array(
"bool" => array(
"must" => array(
"range" => array(
"localtime" => array(
"from" => "2016-06-15T17:43:04.923Z",
"to" => "2016-06-17T17:43:04.923Z",
"include_lower" => "true",
"include_upper" => "true"
)
),
"term" => array(
"query" => "1.2.3.4",
"fields" => array("ip")
),
"query_string" => array(
"query" => "*up*",
"default_field" => array("_all")
)
)
)
);
Why does this error appear?
anyhelp will be appreciated ! thanks!
Your bool/must clause must be a pure array not an associative array:
$qryurl = '<myurl>:<myport>/index/_search?pretty';
$data = array(
"query" => array (
"bool" => array (
"must" => array(
array(
"range" => array (
"localtime" => array (
"from" =>"2016-06-15T17:43:04.923Z",
"to" => "2016-06-17T17:43:04.923Z",
"include_lower" => "true",
"include_upper" => "true"
)
)
),
array(
"term" => array(
"ip" => "1.2.3.4"
)
),
array(
"query_string" => array(
"query" => "*up*",
"default_field" => "_all"
)
)
)
)
)
);

MongoDB very slow result with $lookup

I am using PHP to interact with MongoDB, recently I used $lookup on 5 collection and my query starts taking around 20-25 seconds to get the result. All my collections have testing data which is not more than 30-40 entries. I also used profiling but I am getting message: "Query Not Recording (too Large)"
I am sharing code from my PHP file. I have also tried creating index on field which are creating joins between collection but got same result. Please let me the the best way to create collection structure and to retrieve data from MongoDB.
Thanks in advance.
$collection = new MongoCollection($db, 'col_main');
$condition = array();
$condition[] = array(
'$lookup' => array(
"from" => "col_a",
"localField" => "a",
"foreignField" => "_id",
"as" => "col_a"
)
);
$condition[] = array(
'$lookup' => array(
"from" => "col_b",
"localField" => "b",
"foreignField" => "_id",
"as" => "col_b"
)
);
$condition[] = array(
'$lookup' => array(
"from" => "col_c",
"localField" => "c",
"foreignField" => "_id",
"as" => "col_c"
)
);
$condition[] = array(
'$lookup' => array(
"from" => "col_e",
"localField" => "e",
"foreignField" => "_id",
"as" => "col_e"
)
);
$condition[] = array(
'$lookup' => array(
"from" => "col_f",
"localField" => "f",
"foreignField" => "_id",
"as" => "col_f"
)
);
$condition[] = array(
'$project' => $projection
);
$condition[] = array (
'$unwind' => '$col_a'
);
$condition[] = array (
'$unwind' => '$col_a.translation'
);
$condition[] = array (
'$unwind' => '$col_b'
);
$condition[] = array (
'$unwind' => '$col_b.translation'
);
$condition[] = array (
'$unwind' => '$col_c'
);
$condition[] = array (
'$unwind' => '$col_c.translation'
);
$condition[] = array (
'$unwind' => '$col_e'
);
$condition[] = array (
'$unwind' => '$col_e.staff_name'
);
$condition[] = array (
'$unwind' => '$col_e.staff_surname'
);
$condition[] = array (
'$unwind' => '$col_f'
);
$condition[] = array (
'$unwind' => '$col_f.translation'
);
//Some More conditions
$condition[] = array(
'$match' => array( 'col_b.translation.language' => $val )
);
$condition[] = array(
'$match' => array( 'col_a.translation.language' => $val )
);
$condition[] = array(
'$match' => array( 'col_c.translation.language' => $val )
);
$condition[] = array(
'$match' => array( 'col_f.translation.language' => $val )
);
return $collection->aggregate($condition);
As you are multiplying object using many unwind operations this could produce a huge set of data, every $unwind muliplayes documnet set, so there could be a huge memory consumption and even swapping.
if we have 3 arrays 3 elements each, then we will perform unwind on a, b, c
we will get:
3 documents after first step,
9 documents after 2nd step
27 documents after last step..

PHP Mongo Aggregation only returns _id

I am trying to return a collection of messages grouped by in_reply_to field, I have this code:
$result = $this->db->Message->aggregate(
array(
array(
'$project' => array('message' => 1, 'in_reply_to'=> 1, 'to_user' => 1, 'from_user' => 1)
),
array(
'$group' => array('_id' => '$in_reply_to'),
),
)
);
print_r($result);exit;
the result is:
Array (
[result] => Array (
[0] => Array (
[_id] => MongoId Object (
[$id] => 53a03d43b3f7e236470041a8
)
)
[1] => Array (
[_id] => MongoId Object (
[$id] => 53a03cbdb3f7e2e8350041bb
)
)
)
[ok] => 1
)
Ideally I'd like the entire Message object, but I did think that $project would be used to specify returns fields, even so, I dont get the fields I'm specifying.
Any help is greatly appreciated
In order to get all the messages in the thread you basically want to $push
$result = $this->db->Message->aggregate(
array(
array(
'$group' => array(
'_id' => '$in_reply_to',
'messages' => array(
'$push' => array(
'_id' => '$_id',
'message' => '$message',
'to_user' => '$to_user',
'from_user' =>'$from_user'
)
)
)
)
)
);
MongoDB 2.6 you have the $$ROOT variable that shortens this:
$result = $this->db->Message->aggregate(
array(
array(
'$group' => array(
'_id' => '$in_reply_to',
'messages' => array(
'$push' => '$$ROOT'
)
)
)
)
);
So that puts all of the related messages inside the "messages" array tied to that key.
Just as side note, while you can do this you may as well just sort the results by your "in_reply_to" field and process them that way looking for changes in the value to indicate a new thread.
Sorting with a find would be the fastest way to process, even if it does not conveniently put everything right under the one key.
If you want to get additional fields beside _id field, when using $group operator, you need to include them using some of the available accummulators like $first or $last. You can see the full list on the MongoDB $group documentation page.
The query will look like this:
$result = $this->db->Message->aggregate(
array(
array(
'$project' => array(
'message' => 1,
'in_reply_to'=> 1,
'to_user' => 1,
'from_user' => 1
)
),
array(
'$group' => array(
'_id' => '$in_reply_to',
'message' => array('$first' => '$message'),
'to_user' => ('$first' => '$to_user'),
'from_user' => ('$first' => '$from_user')
),
),
)
);
If the message, to_user and from_user values are same in all documents using $last instead of $first $last will produce the same results.

array picks only the last value to store

The script below apparently uses the API documented at http://www.hasoffers.com/wiki/Offer:create. S the question has (at least) two parts: a) How to store more than one data set within an array. b) Does the API accept it....
When i run the script it only stores the last value inside 'data' how can i get it to store more data at once?
The code below has for example 2 values. one is caled LOLO and the other one is caled LELE.
The output shows only the value LELE.
this is the code.
<?php
header('Content-type: application/json');
$base = 'http://api.hasoffers.com/Api?';
$params = array(
'Format' => 'json'
,'Target' => 'Offer'
,'Method' => 'create'
,'Service' => 'HasOffers'
,'Version' => 2
,'NetworkId' => 'demo'
,'NetworkToken' => '....'
,'data' => array(
'name' => 'LOLO'
,'description' => 'test'
,'offer_url' => 'http://google.nl'
,'preview_url' => 'http://google.nl'
,'expiration_date' => '08-08-2013'
,'name' => 'LELE'
,'description' => 'test'
,'offer_url' => 'http://google.nl'
,'preview_url' => 'http://google.nl'
,'expiration_date' => '08-08-2013'
)
);
$url = $base . http_build_query( $params );
$result = file_get_contents( $url );
print_r( json_decode( $result) );
?>
and this is the output
[request] => stdClass Object
(
[Target] => Offer
[Format] => json
[Service] => HasOffers
[Version] => 2
[Method] => create
[NetworkId] => demo
[NetworkToken] => NETU2nzMw8AYS6EGgjFrjGR88GcSiF
[data] => stdClass Object
(
[name] => LELE
[description] => test
[offer_url] => http://google.nl
[preview_url] => http://google.nl
[expiration_date] => 08-08-2013
)
)
'data' => array(
array (
'name' => 'LOLO',
'description' => 'test',
'offer_url' => 'http://google.nl',
'preview_url' => 'http://google.nl'
'expiration_date' => '08-08-2013'),
array (
'name' => 'LELE',
'description' => 'test',
'offer_url' => 'http://google.nl',
'preview_url' => 'http://google.nl',
'expiration_date' => '08-08-2013'))
,'data' => array( array(
'name' => 'LOLO'
,'description' => 'test'
,'offer_url' => 'http://google.nl'
,'preview_url' => 'http://google.nl'
,'expiration_date' => '08-08-2013'
),
array(
,'name' => 'LELE'
,'description' => 'test'
,'offer_url' => 'http://google.nl'
,'preview_url' => 'http://google.nl'
,'expiration_date' => '08-08-2013'
)
)
You need to add it as a multi dimensional array or else it will overwrite the elements with same key. Please note the array (....) addd inside your array
array( array(
Or you can store it in this way
$data = array();
$data[0] = array('name' => 'LELE'
, 'description' => 'test'
, 'offer_url' => 'http://google.nl'
, 'preview_url' => 'http://google.nl'
, 'expiration_date' => '08-08-2013'
);
$data[1] = array('name' => 'LOLO'
, 'description' => 'test'
, 'offer_url' => 'http://google.nl'
, 'preview_url' => 'http://google.nl'
, 'expiration_date' => '08-08-2013'
);
$params = array(
'Format' => 'json'
, 'Target' => 'Offer'
, 'Method' => 'create'
, 'Service' => 'HasOffers'
, 'Version' => 2
, 'NetworkId' => 'demo'
, 'NetworkToken' => 'NETU2nzMw8AYS6EGgjFrjGR88GcSiF'
, 'data' => $data
);
print_r($params);
// Output
Array
(
[Format] => json
[Target] => Offer
[Method] => create
[Service] => HasOffers
[Version] => 2
[NetworkId] => demo
[NetworkToken] => NETU2nzMw8AYS6EGgjFrjGR88GcSiF
[data] => Array
(
[0] => Array
(
[name] => LELE
[description] => test
[offer_url] => http://google.nl
[preview_url] => http://google.nl
[expiration_date] => 08-08-2013
)
[1] => Array
(
[name] => LOLO
[description] => test
[offer_url] => http://google.nl
[preview_url] => http://google.nl
[expiration_date] => 08-08-2013
)
)
)
a) How to store more than one data set within an array.
$fixed_params = array(
'Format' => 'json'
,'Target' => 'Offer'
,'Method' => 'create'
,'Service' => 'HasOffers'
,'Version' => 2
,'NetworkId' => 'demo'
,'NetworkToken' => '....'
);
$offer_data = array(
array(
'name' => 'LOLO'
,'description' => 'test'
,'offer_url' => 'http://google.nl'
,'preview_url' => 'http://google.nl'
,'expiration_date' => '08-08-2013'
),
array(
'name' => 'LELE'
,'description' => 'test'
,'offer_url' => 'http://google.nl'
,'preview_url' => 'http://google.nl'
,'expiration_date' => '08-08-2013'
)
);
// store all results here
$result = array();
// iterates two times since $offer_data has two elements.
foraech ($offer_data as $offern => $data ){
// store offer's data into fixed_params['data'] element.
$fixed_params['data'] = $data;
$url = $base . http_build_query( $fixed_params );
$result[] = json_decode( file_get_contents( $url ));
}
print_r($result);
b) Does the API accept it....
As I understand the API, it accepts only one create at time. See http://www.hasoffers.com/wiki/Offer:create it says: Creates a new offer.
This is what i did and it works
<?php
// Bestand openen
if (($file = fopen("test2.csv", "r")) !== FALSE) {
// Eerste rij van Excel als value gebruiken
$header = fgetcsv($file, 1000, ";");
// Een loop door Excel file
while (($data = fgetcsv($file, 1000, ";")) !== FALSE) {
// combineer de eerste rij met de gegevens
$combined = array_combine($header,$data);
// Connectie maken met Hasoffers bij elke waarden
$base = 'http://api.hasoffers.com/Api?';
$params = array(
'Format' => 'json'
,'Target' => 'Offer'
,'Method' => 'create'
,'Service' => 'HasOffers'
,'Version' => 2
,'NetworkId' => 'demo'
,'NetworkToken' => '.....'
,'data' => $combined
);
$url = $base . http_build_query( $params );
$result = file_get_contents( $url );
// Tijdelijk printen
print_r( json_encode( $result) );
}
}
?>
I've created a loop including a CSV file.
the problem was that it connected only once with hasoffer and that allowed only one value.

Categories