Forum OpenACS Development: Parsing PHP array

Collapse
Posted by Iuri Sampaio on
Hi there,

What would be the best method to parse the following PHP array?

Is there any ad_proc(s) to do that job on OACS API?

[10/Sep/2013:18:32:32][12268.1873581824][-default:8-] Notice: Array
(
[Experiences] => Array
(
[id] => 56
[name_en] => Great Experience
[name_es] =>
[name_pt] =>
[meta_keyword_en] =>
[meta_keyword_pt] =>
[meta_keyword_es] =>
[meta_description_en] =>
[meta_description_pt] =>
[meta_description_es] =>
[alias] => great-experience
[exp_group] => 0
[provider_id] => 38
[providers_id] => 15
[category] => 33
[sub_category] => 139
[occassion_id] =>
[recipient_id] =>
[sub_occassion] =>
[sub_recipient] =>
[descr_en] =>
[descr_es] =>
[descr_pt] =>
[city_id] => 103
[country] => 42
[expcode] => hsdhsh
[noofpeople] => 0
[duration1_en] =>
[price] => 100.00
[tags_en] =>
[tags_es] =>
[tags_pt] =>
[about_content_en] =>
[about_content_es] =>
[about_content_pt] =>
[more_detail_en] =>
[more_detail_es] =>
[more_detail_pt] =>
[video] =>
[noofviews] => 0
[creation_date] => 2013-09-02 19:06:36
[modified_date] => 0000-00-00 00:00:00
[status] => 1
[featured] => 1
[exchange] => yes
[expire_date] => 2014-09-02 19:06:36
[created_by] => 0
[user_type] => admin
[special_offer] => 0
[special_price] =>
[get_started] => 0
[booking_info_en] =>
[guidelines_en] =>
[weather_en] =>
[participants_en] =>
[spectators_en] =>
[instructors_en] =>
[what_to_wear_en] =>
[getting_there_en] =>
[what_included_en] =>
[what_not_included_en] =>
[valid_until_en] =>
[cancellation_policy_en] =>
[additional_information_en] =>
[cancellation_policy_pt] =>
[cancellation_policy_es] =>
[payment_en] =>
[payment_es] =>
[payment_pt] =>
[additional_information_pt] =>
[additional_information_es] =>
[booking_info_pt] =>
[booking_info_es] =>
[guidelines_pt] =>
[guidelines_es] =>
[weather_pt] =>
[weather_es] =>
[participants_pt] =>
[participants_es] =>
[spectators_pt] =>
[spectators_es] =>
[instructors_pt] =>
[instructors_es] =>
[what_to_wear_pt] =>
[what_to_wear_es] =>
[getting_there_pt] =>
[getting_there_es] =>
[what_included_pt] =>
[what_included_es] =>
[what_not_included_pt] =>
[what_not_included_es] =>
[valid_until_pt] =>
[valid_until_es] =>
[availability_en] =>
[availability_pt] =>
[availability_es] =>
[location_en] =>
[location_pt] =>
[location_es] =>
[duration_en] =>
[duration_pt] =>
[duration_es] =>
[more_participants_en] =>
[more_participants_pt] =>
[more_participants_es] =>
[more_additional_info_en] =>
[more_additional_info_pt] =>
[more_additional_info_es] =>
[how_to_cancel_en] =>
[prepayment_policy_en] =>
[how_to_cancel_es] =>
[prepayment_policy_es] =>
[how_to_cancel_pt] =>
[prepayment_policy_pt] =>
[deposite_pct] => 100.00
[currency] => AUD
[creditcard_require] => 1
[provider_infoarea_en] =>
[provider_infoarea_es] =>
[provider_infoarea_pt] =>
[duration1_es] =>
[duration1_pt] =>
[preferreddate] => yes
)

[city] => Array
(
[name_en] => Australia
[name_pt] =>
[name_es] =>
)

[country] => Array
(
[name_en] => Oceania
[name_pt] => Oceania
[name_es] => Oceania
)

[provider] => Array
(
[name] => Australia
[email] =>
[provider_image] => Fiji1228549.jpg
[phone_number] =>
[address_en] =>
[alias] => australia
[status] =>
)

)

Collapse
2: Re: Parsing PHP array (response to 1)
Posted by Brian Fenton on
Hi Iuri

Not that I know of. But your question does seem quite strange - would it be possible for your PHP source to provide the data in a more neutral format e.g. XML? How does the PHP array get sent to OpenACS?

Sorry, I've more questions than answers!
Brian

Collapse
3: Re: Parsing PHP array (response to 1)
Posted by Dave Bauer on
Like Brian stated, the easiest thing if you have any control over the PHP side, would be to output JSON, I guess, not knowing how exactly the platforms are connected.
Collapse
4: Re: Parsing PHP array (response to 1)
Posted by Iuri Sampaio on
Brian,

PHP array data's sent to OACS via usual HTML form "post".

A bit of context info:

I have control over both sides, not a problem at all, though I haven't developed PHP side, which could turn out a bit tricky to code in the mess that it's already there.

On PHP side, the framework's dummy CakePHP. Data's grabbed from a simple mysql request, put into a html form then sent to OACS side.

i.e.: input type="hidden" name="expArray" value="?php print_r($exFullData[0]); ?"
...

It seems I'll have to do it any way. Whether amending PHP array on CakePHP, or parsing into TCL, when it arrives on OACS, No escapes.

Nevertheless, you guys got a point, it'd be easier if I got a JSON or XML working on PHP side. But I don't want to touch since it wasn't me who developed and it's a CakePHP core piece.

I'd rather use TCL regexp, substr,... let you know whether I succeed or not.

if anyone has a good glimpse about let us know.

The array output to be parsed is here

[10/Sep/2013:18:32:32][12268.1873581824][-default:8-] Notice: Array
(
[Experiences] => Array
(
[id] => 56
[name_en] => Great Experience
[name_es] =>
[name_pt] =>
[meta_keyword_en] =>
[meta_keyword_pt] =>
[meta_keyword_es] =>
[meta_description_en] =>
[meta_description_pt] =>
[meta_description_es] =>
[alias] => great-experience
[exp_group] => 0
[provider_id] => 38
[providers_id] => 15
[category] => 33
[sub_category] => 139
[occassion_id] =>
[recipient_id] =>
[sub_occassion] =>
[sub_recipient] =>
[descr_en] =>
[descr_es] =>
[descr_pt] =>
[city_id] => 103
[country] => 42
[expcode] => hsdhsh
[noofpeople] => 0
[duration1_en] =>
[price] => 100.00
[tags_en] =>
[tags_es] =>
[tags_pt] =>
[about_content_en] =>
[about_content_es] =>
[about_content_pt] =>
[more_detail_en] =>
[more_detail_es] =>
[more_detail_pt] =>
[video] =>
[noofviews] => 0
[creation_date] => 2013-09-02 19:06:36
[modified_date] => 0000-00-00 00:00:00
[status] => 1
[featured] => 1
[exchange] => yes
[expire_date] => 2014-09-02 19:06:36
[created_by] => 0
[user_type] => admin
[special_offer] => 0
[special_price] =>
[get_started] => 0
[booking_info_en] =>
[guidelines_en] =>
[weather_en] =>
[participants_en] =>
[spectators_en] =>
[instructors_en] =>
[what_to_wear_en] =>
[getting_there_en] =>
[what_included_en] =>
[what_not_included_en] =>
[valid_until_en] =>
[cancellation_policy_en] =>
[additional_information_en] =>
[cancellation_policy_pt] =>
[cancellation_policy_es] =>
[payment_en] =>
[payment_es] =>
[payment_pt] =>
[additional_information_pt] =>
[additional_information_es] =>
[booking_info_pt] =>
[booking_info_es] =>
[guidelines_pt] =>
[guidelines_es] =>
[weather_pt] =>
[weather_es] =>
[participants_pt] =>
[participants_es] =>
[spectators_pt] =>
[spectators_es] =>
[instructors_pt] =>
[instructors_es] =>
[what_to_wear_pt] =>
[what_to_wear_es] =>
[getting_there_pt] =>
[getting_there_es] =>
[what_included_pt] =>
[what_included_es] =>
[what_not_included_pt] =>
[what_not_included_es] =>
[valid_until_pt] =>
[valid_until_es] =>
[availability_en] =>
[availability_pt] =>
[availability_es] =>
[location_en] =>
[location_pt] =>
[location_es] =>
[duration_en] =>
[duration_pt] =>
[duration_es] =>
[more_participants_en] =>
[more_participants_pt] =>
[more_participants_es] =>
[more_additional_info_en] =>
[more_additional_info_pt] =>
[more_additional_info_es] =>
[how_to_cancel_en] =>
[prepayment_policy_en] =>
[how_to_cancel_es] =>
[prepayment_policy_es] =>
[how_to_cancel_pt] =>
[prepayment_policy_pt] =>
[deposite_pct] => 100.00
[currency] => AUD
[creditcard_require] => 1
[provider_infoarea_en] =>
[provider_infoarea_es] =>
[provider_infoarea_pt] =>
[duration1_es] =>
[duration1_pt] =>
[preferreddate] => yes
)

[city] => Array
(
[name_en] => Australia
[name_pt] =>
[name_es] =>
)

[country] => Array
(
[name_en] => Oceania
[name_pt] => Oceania
[name_es] => Oceania
)

[provider] => Array
(
[name] => Australia
[email] =>
[provider_image] => Fiji1228549.jpg
[phone_number] =>
[address_en] =>
[alias] => australia
[status] =>
)

)

Collapse
5: Re: Parsing PHP array (response to 1)
Posted by Torben Brosten on

Iuri,

Perhaps a quick way would be to use q-forms package to convert the array to a tcl array.

Rename the php code that receives the post to php-post-name-2

Put your tcl code in place of the receiving php.

# make a tcl array of posted data (already quoted):

qf_get_inputs_as_array input_array2

# put your code here


# pass the data back to php app via internal redirect:

set query_key_list [array names input_array]
foreach query_key $query_key_list {
    rp_form_put $query_key [ad_urlencode $input_array(${query_key})]
}
rp_internal_redirect /packages/q-wiki/www/example-php-post-name-2
ad_script_abort

cheers,

Collapse
6: Re: Parsing PHP array (response to 1)
Posted by Torben Brosten on
The cvs version of q-forms isn't uptodate. Grab it from here:

http://github.com/tekbasse/q-forms

https://openacs.org/xowiki/q-forms

Collapse
7: Re: Parsing PHP array (response to 1)
Posted by Torben Brosten on

Ops, that example code has a typo. It should be:

# make a tcl array of posted data (already quoted):

qf_get_inputs_as_array input_array

# put your code here

# However the form represents the data, it is received as if it were written in tcl like this:

#  set input_array(name) $value

# pass the data back to php app via internal redirect:

set query_key_list [array names input_array]
foreach query_key $query_key_list {
    rp_form_put $query_key [ad_urlencode $input_array(${query_key})]
}
rp_internal_redirect /packages/q-wiki/www/example-php-post-name-2
ad_script_abort
Collapse
8: Re: Parsing PHP array (response to 1)
Posted by Iuri Sampaio on
Thanks Torben,

I'm reading your references ...

I'm hopping to give a feedback soon!

Collapse
9: Re: Parsing PHP array (response to 1)
Posted by Iuri Sampaio on
Torben,

There's something wrong with ad_proc qf_get_inputs_as_array. Its behavior's unstable as sometimes it works fine but other just doesn't.

Sometimes I get errors as qf_get_inputs_as_array wasn't installed.

"... invalid command name "qf_get_inputs_as_array ..."

Have you ever face anything similar to that?
How did you fix it?

The chunk of code that generates logs on error.log is bellow:

1. Logs with ad_proc working fine
2. Logs with ad_proc returning error

...

qf_get_inputs_as_array $expArray


set query_key_list [array names $expArray]

# ns_log Notice "TETETE $query_key_list"
foreach query_key $query_key_list {
ns_log Notice "$query_key \n"
}

#####
##1
#####

[20/Sep/2013:09:44:03][18605.615929600][-default:3-] Notice: FORM
Unamed set:
firstname = Iuri
lastname = Sampaio
phone = 9876545678
email = mailto:iuri.sampaio@iurix.com
newsletter_p =
date1_day = 24
date1_month = 09
date1_year = 2013
date1 = 09/24/2013
date2_day = 25
date2_month = 09
date2_year = 2013
date2 = 09/25/2013
date3_day = 27
date3_month = 09
date3_year = 2013
date3 = 09/27/2013
message = fvsdvaeveav
locale =
product = Great Experience
expArray = Array
...

[20/Sep/2013:09:44:03][18605.615929600][-default:3-] Notice: date2
[20/Sep/2013:09:44:03][18605.615929600][-default:3-] Notice: date2_year
[20/Sep/2013:09:44:03][18605.615929600][-default:3-] Notice: date3
[20/Sep/2013:09:44:03][18605.615929600][-default:3-] Notice: firstname
[20/Sep/2013:09:44:03][18605.615929600][-default:3-] Notice: newsletter_p
[20/Sep/2013:09:44:03][18605.615929600][-default:3-] Notice: date1_month
[20/Sep/2013:09:44:03][18605.615929600][-default:3-] Notice: lastname
[20/Sep/2013:09:44:03][18605.615929600][-default:3-] Notice: date2_month
[20/Sep/2013:09:44:03][18605.615929600][-default:3-] Notice: date3_month
[20/Sep/2013:09:44:03][18605.615929600][-default:3-] Notice: date1_year
[20/Sep/2013:09:44:03][18605.615929600][-default:3-] Notice: locale
[20/Sep/2013:09:44:03][18605.615929600][-default:3-] Notice: email
[20/Sep/2013:09:44:03][18605.615929600][-default:3-] Notice: date3_year
[20/Sep/2013:09:44:03][18605.615929600][-default:3-] Notice: product
[20/Sep/2013:09:44:03][18605.615929600][-default:3-] Notice: phone
[20/Sep/2013:09:44:03][18605.615929600][-default:3-] Notice: date1_day
[20/Sep/2013:09:44:03][18605.615929600][-default:3-] Notice: expArray
[20/Sep/2013:09:44:03][18605.615929600][-default:3-] Notice: date2_day
[20/Sep/2013:09:44:03][18605.615929600][-default:3-] Notice: message
[20/Sep/2013:09:44:03][18605.615929600][-default:3-] Notice: date3_day
[20/Sep/2013:09:44:03][18605.615929600][-default:3-] Notice: date1
[20/Sep/2013:09:44:03][18605.615929600][-default:3-] Notice: Send Email

#####
## 2.
#####

But sometimes it doesn't. And I have no clue why that instability.

[20/Sep/2013:09:41:18][18605.616986368][-default:0-] Error: POST http://ecommerce.natopia.com/notifications/check-availability?
referred by "http://dev.natopia.com/experiences/details/great-experience";
invalid command name "qf_get_inputs_as_array"
while executing
"qf_get_inputs_as_array $expArray"
("uplevel" body line 38)
invoked from within
"uplevel {
ad_page_contract {
} {
{firstname ""}
{lastname ""}
{email ""}
{product ""}
{phone ""}
{email ""}
{newsletter_p ..."
(procedure "code::tcl::/var/www/natopia/packages/natopia-notifications/w..." line 2)
invoked from within
"code::tcl::$__adp_stub"
("uplevel" body line 12)
invoked from within
"uplevel {

Collapse
10: Re: Parsing PHP array (response to 1)
Posted by Torben Brosten on
Hi Iuri,

I haven't seen the "invalid command name" error; It's been in use on sites for over a year.

qf_get_inputs_as_array is designed to be used outside of ad_page_contract in the same way that ad_form does not use ad_page_contract.

Maybe executing qf_get_inputs_as_array inside of ad_page_contract causes the issue.

Collapse
11: Re: Parsing PHP array (response to 1)
Posted by Torben Brosten on
A quick way to check would be to remove ad_page_contract, and set defaults before calling qf_input_as_array.
Collapse
12: Re: Parsing PHP array (response to 11)
Posted by Iuri Sampaio on
Hi Torben,

I didn't write qf_input_as_array within ad_page_contract.

However I did write within it all form variables, as in

ad_page_contract {
} {
{firstname ""}
{lastname ""}
{email ""}
{product ""}
{phone ""}
{email ""}
{newsletter_p ""}
{date1_day ""}
{date1_month ""}
{date1_year ""}
{date2_day ""}
{date2_month ""}
{date2_year ""}
{date3_day ""}
{date3_month ""}
{date3_year ""}
{message ""}
{locale ""}
{expArray ""}
}

qf_input_as_array was never inside but outside page contract.

Nevertheless, you notes made me wise about variables within it. Thus, I removed them and the ad_proc is stable since then.
Don't ask me why, cause I have no idea.

I just was used to code like that: every time I have a form I was used to add form variables, all them, within ad_page_contract

Thanks for the tip.
Sorry for my delay, I was in Utah up in the mountains without computer.