Waarden omzetten binnen een geneste array
array
( 'body' => '{
"contactName":"Bart",
"contactPhone":"+32478000000",
"contactEmail":"info@………be",
"contactLanguage":"nl",
"regionId":5,
"passengers":
[{
"name":"Bart",
"child":"",
"weight":"90",
"paymentType":"COUP",
"paymentReference":"1234 5678 90"},
{
"name":"Annelies",
"child":"",
"weight":"70",
"paymentType":"WIRE"},
{
"name":"Amber",
"child":"on",
"weight":"35",
"paymentType":"WIRE"
}
]}
',
)
Dit moet er gebeuren:
Als “child” empty is, moet het de boolean false dragen;Als “child” de een string ‘on’ bevat, moet het de boolean true dragen;“Weight” moet een integer zijn, geen string, dus zonder haakjes.
Vermits dat dit allemaal in de geneste array zit, weet ik niet goed hoe ik dit moet omzetten. Waarschijnlijk een loop creëren en in de loop de waardes omzetten.
Ik ben nog niet lang bezig met php, dus alle hulp is erg welkom!
Gewijzigd op 26/07/2021 08:04:12 door Bart Raymakers
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
$nested = array (
'body' => array (
"contactName" => "Bart R",
"contactPhone" => "+324",
"contactEmail" => "[email protected]",
"contactLanguage" =>"nl",
"regionId" => 5,
"passengers" => array(
array(
"name" => "Bart",
"child" => "",
"weight" => "90",
"paymentType" => "COUP",
"paymentReference" => "1234 5678 90"
),
array(
"name" => "Annelies",
"child" => "",
"weight" => "70",
"paymentType" => "WIRE"
),
array(
"name" => "Amber",
"child" => "on",
"weight" => "35",
"paymentType" => "WIRE"
)
)
)
);
echo '<pre>' . print_r( $nested, TRUE ) . '</pre>';
?>
<pre>Als 'child' empty is, moet het de boolean false dragen;
Als 'child' de een string on bevat, moet het de boolean true dragen;
'Weight' moet een integer zijn, geen string, dus zonder haakjes.
</pre>
<?php
foreach ( $nested['body']['passengers'] as $key => $value )
{
echo '<pre>' . print_r( $value, TRUE ) . '</pre>';
$nested['body']['passengers'][$key]['child'] = $value['child'] == '' ? (boolean)FALSE : (boolean)TRUE;
$nested['body']['passengers'][$key]['child'] = $value['child'] == 'on' ? (boolean)TRUE : (boolean)FALSE;
echo 'Gewicht ' . $value['weight'] . ' : ' . ( is_integer( $value['weight']) ? 'is integer' : 'is geen integer' ) . '<br /><br />';
$nested['body']['passengers'][$key]['weight'] = $value['weight'] = (int)$value['weight'];
}
echo '<pre>' . print_r( $nested, TRUE ) . '</pre>';
foreach ( $nested['body']['passengers'] as $key => $value )
{
echo 'Kind ' . $value['name'] . ' : ' . ( $value['child'] == TRUE ? 'is kind' : 'is geen kind' ) . '<br />';
echo 'Gewicht ' . $value['weight'] . ' : ' . ( is_integer( $value['weight']) ? 'is integer' : 'is geen integer' ) . '<br /><br />';
}
?>
$nested = array (
'body' => array (
"contactName" => "Bart R",
"contactPhone" => "+324",
"contactEmail" => "[email protected]",
"contactLanguage" =>"nl",
"regionId" => 5,
"passengers" => array(
array(
"name" => "Bart",
"child" => "",
"weight" => "90",
"paymentType" => "COUP",
"paymentReference" => "1234 5678 90"
),
array(
"name" => "Annelies",
"child" => "",
"weight" => "70",
"paymentType" => "WIRE"
),
array(
"name" => "Amber",
"child" => "on",
"weight" => "35",
"paymentType" => "WIRE"
)
)
)
);
echo '<pre>' . print_r( $nested, TRUE ) . '</pre>';
?>
<pre>Als 'child' empty is, moet het de boolean false dragen;
Als 'child' de een string on bevat, moet het de boolean true dragen;
'Weight' moet een integer zijn, geen string, dus zonder haakjes.
</pre>
<?php
foreach ( $nested['body']['passengers'] as $key => $value )
{
echo '<pre>' . print_r( $value, TRUE ) . '</pre>';
$nested['body']['passengers'][$key]['child'] = $value['child'] == '' ? (boolean)FALSE : (boolean)TRUE;
$nested['body']['passengers'][$key]['child'] = $value['child'] == 'on' ? (boolean)TRUE : (boolean)FALSE;
echo 'Gewicht ' . $value['weight'] . ' : ' . ( is_integer( $value['weight']) ? 'is integer' : 'is geen integer' ) . '<br /><br />';
$nested['body']['passengers'][$key]['weight'] = $value['weight'] = (int)$value['weight'];
}
echo '<pre>' . print_r( $nested, TRUE ) . '</pre>';
foreach ( $nested['body']['passengers'] as $key => $value )
{
echo 'Kind ' . $value['name'] . ' : ' . ( $value['child'] == TRUE ? 'is kind' : 'is geen kind' ) . '<br />';
echo 'Gewicht ' . $value['weight'] . ' : ' . ( is_integer( $value['weight']) ? 'is integer' : 'is geen integer' ) . '<br /><br />';
}
?>
Gewijzigd op 26/07/2021 09:25:13 door Adoptive Solution
Toevoeging op 25/07/2021 21:26:32:
Helaas, het wil niet werken. Nuja, ik begrijp de code ook maar half, dus heb ik hem gewoon geïntegreerd.
Hieronder de feitelijke plugin (WordPress), misschien kan jij zien waar het fout loopt?
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
function piotnetforms_send_request_bart( $form_submission ) {
$regio = [
'regionId' => $form_submission['fields']['region_Id']['value'],
];
$regio = $regio[regionId]; // regio is een array!!!
$regio = intval($regio); // regio moet een integer worden
$body = [
'contactName' => $form_submission['fields']['contactName']['value'],
'contactPhone' => $form_submission['fields']['contactPhone']['value'],
'contactEmail' => $form_submission['fields']['contactEmail']['value'],
'contactLanguage' => $form_submission['fields']['contactLanguage']['value'],
'contactCity' => $form_submission['fields']['contactCity']['value'],
'regionId' => $regio,
'passengers' => $form_submission['fields']['passengers']['value'],
];
// begin omzetting
foreach ( $body['body']['passengers'] as $key => $value )
{
echo '<pre>' . print_r( $value, TRUE ) . '</pre>';
$nested['body']['passengers'][$key]['child'] = $value['child'] == '' ? (boolean)FALSE : (boolean)TRUE;
$nested['body']['passengers'][$key]['child'] = $value['child'] == 'on' ? (boolean)TRUE : (boolean)FALSE;
echo 'Gewicht ' . $value['weight'] . ' : ' . ( is_integer( $value['weight']) ? 'is integer' : 'is geen integer' ) . '<br /><br />';
$nested['body']['passengers'][$key]['weight'] = $value['weight'] = (int)$value['weight'];
}
echo '<pre>' . print_r( $nested, TRUE ) . '</pre>';
foreach ( $body['body']['passengers'] as $key => $value )
{
echo 'Kind ' . $value['name'] . ' : ' . ( $value['child'] == TRUE ? 'is kind' : 'is geen kind' ) . '<br />';
echo 'Gewicht ' . $value['weight'] . ' : ' . ( is_integer( $value['weight']) ? 'is integer' : 'is geen integer' ) . '<br /><br />';
}
// einde omzetting
$request = [
'body' => json_encode($body),
];
$content = var_export($request, true);
$to = '[email protected]';
$subject = 'The subject';
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $content, $headers );
}
add_action('piotnetforms/form_builder/new_record_v2', 'piotnetforms_send_request_bart');
$regio = [
'regionId' => $form_submission['fields']['region_Id']['value'],
];
$regio = $regio[regionId]; // regio is een array!!!
$regio = intval($regio); // regio moet een integer worden
$body = [
'contactName' => $form_submission['fields']['contactName']['value'],
'contactPhone' => $form_submission['fields']['contactPhone']['value'],
'contactEmail' => $form_submission['fields']['contactEmail']['value'],
'contactLanguage' => $form_submission['fields']['contactLanguage']['value'],
'contactCity' => $form_submission['fields']['contactCity']['value'],
'regionId' => $regio,
'passengers' => $form_submission['fields']['passengers']['value'],
];
// begin omzetting
foreach ( $body['body']['passengers'] as $key => $value )
{
echo '<pre>' . print_r( $value, TRUE ) . '</pre>';
$nested['body']['passengers'][$key]['child'] = $value['child'] == '' ? (boolean)FALSE : (boolean)TRUE;
$nested['body']['passengers'][$key]['child'] = $value['child'] == 'on' ? (boolean)TRUE : (boolean)FALSE;
echo 'Gewicht ' . $value['weight'] . ' : ' . ( is_integer( $value['weight']) ? 'is integer' : 'is geen integer' ) . '<br /><br />';
$nested['body']['passengers'][$key]['weight'] = $value['weight'] = (int)$value['weight'];
}
echo '<pre>' . print_r( $nested, TRUE ) . '</pre>';
foreach ( $body['body']['passengers'] as $key => $value )
{
echo 'Kind ' . $value['name'] . ' : ' . ( $value['child'] == TRUE ? 'is kind' : 'is geen kind' ) . '<br />';
echo 'Gewicht ' . $value['weight'] . ' : ' . ( is_integer( $value['weight']) ? 'is integer' : 'is geen integer' ) . '<br /><br />';
}
// einde omzetting
$request = [
'body' => json_encode($body),
];
$content = var_export($request, true);
$to = '[email protected]';
$subject = 'The subject';
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $content, $headers );
}
add_action('piotnetforms/form_builder/new_record_v2', 'piotnetforms_send_request_bart');
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
$body_origineel = [
'contactName' => $form_submission['fields']['contactName']['value'],
'contactPhone' => $form_submission['fields']['contactPhone']['value'],
'contactEmail' => $form_submission['fields']['contactEmail']['value'],
'contactLanguage' => $form_submission['fields']['contactLanguage']['value'],
'contactCity' => $form_submission['fields']['contactCity']['value'],
'regionId' => $regio,
'passengers' => $form_submission['fields']['passengers']['value']
];
$body = [
"contactName" => "Bart R",
"contactPhone" => "+324",
"contactEmail" => "[email protected]",
"contactLanguage" =>"nl",
"regionId" => 5,
"passengers" => [
[
"name" => "Bart",
"child" => "",
"weight" => "90",
"paymentType" => "COUP",
"paymentReference" => "1234 5678 90"
],
[
"name" => "Annelies",
"child" => "",
"weight" => "70",
"paymentType" => "WIRE"
],
[
"name" => "Amber",
"child" => "on",
"weight" => "35",
"paymentType" => "WIRE"
]
]
];
echo '<pre>' . print_r( $body, TRUE ) . '</pre>';
?>
<pre>Als 'child' empty is, moet het de boolean false dragen;
Als 'child' de een string on bevat, moet het de boolean true dragen;
'Weight' moet een integer zijn, geen string, dus zonder haakjes.
</pre>
<?php
foreach ( $body['passengers'] as $key => $value )
{
echo '<pre>' . print_r( $value, TRUE ) . '</pre>';
$body['passengers'][$key]['child'] = $value['child'] == '' ? (boolean)FALSE : (boolean)TRUE;
$body['passengers'][$key]['child'] = $value['child'] == 'on' ? (boolean)TRUE : (boolean)FALSE;
echo $value['name'] . ' : ' . ( $value['child'] == TRUE ? 'is een kind' : 'is geen kind' ) . '<br />';
echo 'Gewicht ' . $value['weight'] . ' : ' . ( is_integer( $value['weight']) ? 'is een integer' : 'is geen integer' ) . '<br /><br />';
$body['passengers'][$key]['weight'] = $value['weight'] = (int)$value['weight'];
}
echo '<pre>' . print_r( $body, TRUE ) . '</pre>';
foreach ( $body['passengers'] as $key => $value )
{
echo $value['name'] . ' : ' . ( $value['child'] == TRUE ? 'is een kind' : 'is geen kind' ) . '<br />';
echo 'Gewicht ' . $value['weight'] . ' : ' . ( is_integer( $value['weight']) ? 'is een integer' : 'is geen integer' ) . '<br /><br />';
}
?>
$body_origineel = [
'contactName' => $form_submission['fields']['contactName']['value'],
'contactPhone' => $form_submission['fields']['contactPhone']['value'],
'contactEmail' => $form_submission['fields']['contactEmail']['value'],
'contactLanguage' => $form_submission['fields']['contactLanguage']['value'],
'contactCity' => $form_submission['fields']['contactCity']['value'],
'regionId' => $regio,
'passengers' => $form_submission['fields']['passengers']['value']
];
$body = [
"contactName" => "Bart R",
"contactPhone" => "+324",
"contactEmail" => "[email protected]",
"contactLanguage" =>"nl",
"regionId" => 5,
"passengers" => [
[
"name" => "Bart",
"child" => "",
"weight" => "90",
"paymentType" => "COUP",
"paymentReference" => "1234 5678 90"
],
[
"name" => "Annelies",
"child" => "",
"weight" => "70",
"paymentType" => "WIRE"
],
[
"name" => "Amber",
"child" => "on",
"weight" => "35",
"paymentType" => "WIRE"
]
]
];
echo '<pre>' . print_r( $body, TRUE ) . '</pre>';
?>
<pre>Als 'child' empty is, moet het de boolean false dragen;
Als 'child' de een string on bevat, moet het de boolean true dragen;
'Weight' moet een integer zijn, geen string, dus zonder haakjes.
</pre>
<?php
foreach ( $body['passengers'] as $key => $value )
{
echo '<pre>' . print_r( $value, TRUE ) . '</pre>';
$body['passengers'][$key]['child'] = $value['child'] == '' ? (boolean)FALSE : (boolean)TRUE;
$body['passengers'][$key]['child'] = $value['child'] == 'on' ? (boolean)TRUE : (boolean)FALSE;
echo $value['name'] . ' : ' . ( $value['child'] == TRUE ? 'is een kind' : 'is geen kind' ) . '<br />';
echo 'Gewicht ' . $value['weight'] . ' : ' . ( is_integer( $value['weight']) ? 'is een integer' : 'is geen integer' ) . '<br /><br />';
$body['passengers'][$key]['weight'] = $value['weight'] = (int)$value['weight'];
}
echo '<pre>' . print_r( $body, TRUE ) . '</pre>';
foreach ( $body['passengers'] as $key => $value )
{
echo $value['name'] . ' : ' . ( $value['child'] == TRUE ? 'is een kind' : 'is geen kind' ) . '<br />';
echo 'Gewicht ' . $value['weight'] . ' : ' . ( is_integer( $value['weight']) ? 'is een integer' : 'is geen integer' ) . '<br /><br />';
}
?>
Gewijzigd op 26/07/2021 09:25:46 door Adoptive Solution
Als ik jouw code als separate php-file uitvoer, dan zie ik dat de waardes aangepast zijn. Onderstaande is letterlijk wat er gemaild wordt:
array ( 'body' => '{"contactName":"Bart","contactPhone":"+3247000000","contactEmail":"[email protected]","contactLanguage":"nl","contactCity":"Diest","regionId":2,"passengers":[{"name":"Bart","child":"","weight":"80","paymentType":"WIRE"},{"name":"Amber","child":"on","weight":"30","paymentType":"WIRE"}]}', )
Bij het uitvoeren van mijn code lijkt de $content (zie mijn vorige bericht) ongewijzigd te blijven.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/**
* Plugin name: Query APIs
* Plugin URI: https://www.webdesignbart.be
* Description: Pass information from external APIs in WordPress - json
* Author: Bart Raymakers
* Author URI: https://www.webdesignbart.be
* version: 0.1.0
* License: GPL2 or later.
* text-domain: query-apis
*/
function piotnetforms_send_request_bart( $form_submission ) {
$regio = [
'regionId' => $form_submission['fields']['region_Id']['value'],
];
$regio = $regio[regionId]; // regio is een array!!!
$regio = intval($regio); // regio moet een integer worden
$body = [
'contactName' => $form_submission['fields']['contactName']['value'],
'contactPhone' => $form_submission['fields']['contactPhone']['value'],
'contactEmail' => $form_submission['fields']['contactEmail']['value'],
'contactLanguage' => $form_submission['fields']['contactLanguage']['value'],
'contactCity' => $form_submission['fields']['contactCity']['value'],
'regionId' => $regio,
'passengers' => $form_submission['fields']['passengers']['value'],
];
$request = [
'body' => json_encode($body),
];
// begin omzetting
foreach ( $request['body']['passengers'] as $key => $value )
{
$request['body']['passengers'][$key]['child'] = $value['child'] == '' ? (boolean)FALSE : (boolean)TRUE;
$request['body']['passengers'][$key]['child'] = $value['child'] == 'on' ? (boolean)TRUE : (boolean)FALSE;
$request['body']['passengers'][$key]['weight'] = $value['weight'] = (int)$value['weight'];
}
// einde omzetting
$content = var_export($request, true);
$to = '[email protected]';
$subject = 'The subject';
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $content, $headers );
}
add_action('piotnetforms/form_builder/new_record_v2', 'piotnetforms_send_request_bart');
[/code]
/**
* Plugin name: Query APIs
* Plugin URI: https://www.webdesignbart.be
* Description: Pass information from external APIs in WordPress - json
* Author: Bart Raymakers
* Author URI: https://www.webdesignbart.be
* version: 0.1.0
* License: GPL2 or later.
* text-domain: query-apis
*/
function piotnetforms_send_request_bart( $form_submission ) {
$regio = [
'regionId' => $form_submission['fields']['region_Id']['value'],
];
$regio = $regio[regionId]; // regio is een array!!!
$regio = intval($regio); // regio moet een integer worden
$body = [
'contactName' => $form_submission['fields']['contactName']['value'],
'contactPhone' => $form_submission['fields']['contactPhone']['value'],
'contactEmail' => $form_submission['fields']['contactEmail']['value'],
'contactLanguage' => $form_submission['fields']['contactLanguage']['value'],
'contactCity' => $form_submission['fields']['contactCity']['value'],
'regionId' => $regio,
'passengers' => $form_submission['fields']['passengers']['value'],
];
$request = [
'body' => json_encode($body),
];
// begin omzetting
foreach ( $request['body']['passengers'] as $key => $value )
{
$request['body']['passengers'][$key]['child'] = $value['child'] == '' ? (boolean)FALSE : (boolean)TRUE;
$request['body']['passengers'][$key]['child'] = $value['child'] == 'on' ? (boolean)TRUE : (boolean)FALSE;
$request['body']['passengers'][$key]['weight'] = $value['weight'] = (int)$value['weight'];
}
// einde omzetting
$content = var_export($request, true);
$to = '[email protected]';
$subject = 'The subject';
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $content, $headers );
}
add_action('piotnetforms/form_builder/new_record_v2', 'piotnetforms_send_request_bart');
[/code]
Gewijzigd op 26/07/2021 08:06:25 door Bart Raymakers
Zou het niet beter zijn om deze contactinfo te verwijderen van dit forum.
Goede opmerking Jan. Ik heb mijn posts gewijzigd.
En die foreach moet met $body. Niet met $request. Je kunt niet een json op die manier aanpassen.
Gewijzigd op 26/07/2021 09:37:17 door Adoptive Solution
(boolean)TRUE
Waarom zou je hier een boolean typecasten naar een boolean?????
En wat is dit voor constructie?
$body['passengers'][$key]['weight'] = $value['weight'] = (int)$value['weight'];
En waarom dit:
$request['body']['passengers'][$key]['child'] = $value['child'] == 'on' ? (boolean)TRUE : (boolean)FALSE;
niet gewoon zo?
$request['body']['passengers'][$key]['child'] = ($value['child'] == 'on');
Code (php)
1
2
2
$nested['body']['passengers'][$key]['weight'] = $value['weight'] = (int)$value['weight'];
$body['passengers'][$key]['weight'] = $value['weight'] = (int)$value['weight'];
$body['passengers'][$key]['weight'] = $value['weight'] = (int)$value['weight'];
Moet zijn :
Code (php)
1
2
2
$nested['body']['passengers'][$key]['weight'] = (int)$value['weight'];
$body['passengers'][$key]['weight'] = (int)$value['weight'];
$body['passengers'][$key]['weight'] = (int)$value['weight'];
Gewijzigd op 26/07/2021 16:39:33 door Adoptive Solution
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
/**
* Plugin name: Query APIs
* Plugin URI: https://www.webdesignbart.be
* Description: Pass information from external APIs in WordPress - json
* Author: Bart Raymakers
* Author URI: https://www.webdesignbart.be
* version: 0.1.0
* License: GPL2 or later.
* text-domain: query-apis
*/
function piotnetforms_send_request_bart( $form_submission ) {
$regio = [
'regionId' => $form_submission['fields']['region_Id']['value'],
];
$regio = $regio[regionId]; // regio is een array!!!
$regio = intval($regio); // regio moet een integer worden
$body = [
'contactName' => $form_submission['fields']['contactName']['value'],
'contactPhone' => $form_submission['fields']['contactPhone']['value'],
'contactEmail' => $form_submission['fields']['contactEmail']['value'],
'contactLanguage' => $form_submission['fields']['contactLanguage']['value'],
'contactCity' => $form_submission['fields']['contactCity']['value'],
'regionId' => $regio,
'passengers' => $form_submission['fields']['passengers']['value'],
];
// begin omzetting
foreach ( $request['body']['passengers'] as $key => $value )
{
$request['body']['passengers'][$key]['child'] = $value['child'] == '' ? (boolean)FALSE : (boolean)TRUE;
$request['body']['passengers'][$key]['child'] = $value['child'] == 'on' ? (boolean)TRUE : (boolean)FALSE;
$request['body']['passengers'][$key]['weight'] = $value['weight'] = (int)$value['weight'];
}
// einde omzetting
$request = json_encode([ 'body' => $body ] );
echo $request;
$content = var_export($request, true);
$to = '[email protected]';
$subject = 'The subject';
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $content, $headers );
}
add_action('piotnetforms/form_builder/new_record_v2', 'piotnetforms_send_request_bart');[/code]
/**
* Plugin name: Query APIs
* Plugin URI: https://www.webdesignbart.be
* Description: Pass information from external APIs in WordPress - json
* Author: Bart Raymakers
* Author URI: https://www.webdesignbart.be
* version: 0.1.0
* License: GPL2 or later.
* text-domain: query-apis
*/
function piotnetforms_send_request_bart( $form_submission ) {
$regio = [
'regionId' => $form_submission['fields']['region_Id']['value'],
];
$regio = $regio[regionId]; // regio is een array!!!
$regio = intval($regio); // regio moet een integer worden
$body = [
'contactName' => $form_submission['fields']['contactName']['value'],
'contactPhone' => $form_submission['fields']['contactPhone']['value'],
'contactEmail' => $form_submission['fields']['contactEmail']['value'],
'contactLanguage' => $form_submission['fields']['contactLanguage']['value'],
'contactCity' => $form_submission['fields']['contactCity']['value'],
'regionId' => $regio,
'passengers' => $form_submission['fields']['passengers']['value'],
];
// begin omzetting
foreach ( $request['body']['passengers'] as $key => $value )
{
$request['body']['passengers'][$key]['child'] = $value['child'] == '' ? (boolean)FALSE : (boolean)TRUE;
$request['body']['passengers'][$key]['child'] = $value['child'] == 'on' ? (boolean)TRUE : (boolean)FALSE;
$request['body']['passengers'][$key]['weight'] = $value['weight'] = (int)$value['weight'];
}
// einde omzetting
$request = json_encode([ 'body' => $body ] );
echo $request;
$content = var_export($request, true);
$to = '[email protected]';
$subject = 'The subject';
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $content, $headers );
}
add_action('piotnetforms/form_builder/new_record_v2', 'piotnetforms_send_request_bart');[/code]
Waarom wil je de string 'on' vervangen door de boolean true als je vervolgens de ingevulde formuliergegevens gaat e-mailen? Dan kun je beter child vervangen door kind en 'on' door 'ja' in het HTML-formulier.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
// begin omzetting
foreach ( $body['passengers'] as $key => $value )
{
$body['passengers'][$key]['child'] = $value['child'] == '' ? FALSE : TRUE;
$body['passengers'][$key]['child'] = $value['child'] == 'on' ? TRUE : FALSE;
$body['passengers'][$key]['weight'] = (int)$value['weight'];
}
// einde omzetting
$request = json_encode([ 'body' => $body ] );
echo $request;
foreach ( $body['passengers'] as $key => $value )
{
$body['passengers'][$key]['child'] = $value['child'] == '' ? FALSE : TRUE;
$body['passengers'][$key]['child'] = $value['child'] == 'on' ? TRUE : FALSE;
$body['passengers'][$key]['weight'] = (int)$value['weight'];
}
// einde omzetting
$request = json_encode([ 'body' => $body ] );
echo $request;
Het resultaat ziet er zo uit
Code (php)
1
{"body":{"contactName":"Bart R","contactPhone":"+32.....585","contactEmail":"[email protected]","contactLanguage":"nl","regionId":5,"passengers":[{"name":"Bart","child":false,"weight":90,"paymentType":"COUP","paymentReference":"1234 5678 90"},{"name":"Annelies","child":false,"weight":70,"paymentType":"WIRE"},{"name":"Amber","child":true,"weight":35,"paymentType":"WIRE"}]}}