2 arrays with simplexml load string failing

NetMonkey

Hey guys,

Sorry, php noob here, but can grasp new concepts rather quickly. I've been in IT a long time and this is my first time trying to rewrite this much php code. I've been working on this php script for 7 days now and think I'm getting close. I've seen most every syntax error, have learned a lot and morphed the code into what it is now.

The script I'm hacking was written back in 09 with 16 (static) user data fields in an array to test register a user at a third-party website via SSL with curl and a simple xml load string. If you get a success response, all went well with the other server. If not, all you get is a fail response.

The first 6 data fields will still remain static, but the last 10 will always vary because I'm querying a WP user DB to get user data. The script just queries the WP user DB creates an array and posts to the other server and registers the user, so the user that registered with the first site doesn't have to go through the whole registration process over again. This is totally G-rated. It's for a consumer discounts site.

Here's what I'm trying to get the code to do. Create an array, query the DB and then pass the first static array data and the user array data to the post string and get a success response from the other server.

This is the whole script that's failing except for opening and closing tags and I'm getting a syntax error, unexpected T_VARIABLE on line 42:
//$query_vals = array();

Error404

The reason for that syntax error is you're missing a semi-colon on the line above $query_vals = array(); . After you fix that, there are 3 other errors. First, your foreach loops aren't doing anything as you're immediately ending them with a ; and have no { } around them. I'm guessing you're trying to write nested foreach loops, which should be:

foreach($query_vals as $key => $value) {
// since $key and $value are used in the outer loop
// you need to use different variable names for the inner loop
// otherwise you'll over-write $key and $value each time!
foreach($current_user_info as $key2 => $value2) {
}
}

Second, you have a randomly-placed { on the line $result = array_merge($query_vals, $current_user_info); . Third, you have the closing randomly-placed } just after $postdata .= $key.'='.urlencode($value).'&';

NetMonkey

Now I'm getting a server 500 error. This is my code after several suggestions in the past hour:

this function has infinite recursion (i.e. $current_user = wp_get_current_user();). also the variable scope is not established ($current_user_info is only available within wp_get_current_user()) nor is there a return value.

this function has infinite recursion (i.e. $current_user = wp_get_current_user();). also the variable scope is not established ($current_user_info is only available within wp_get_current_user()) nor is there a return value.

The code you saw there is a recent change from 4 hours ago that was suggested by another developer who said my (other code) I had was wrong. It seems I keep making code changes based on suggestions, and nothing seems to work and then others say the last code change was wrong.

Php is one of the most difficult things to master I've ever attempted. I'm no closer now that I was a week ago because no code suggestions I've received from about seven developers has worked. I don't know what to think, or how this is going to get resolved.

I'm not sure what the answer here is.

ShrineDesigns

yea the initial hurdles of learning php may seem overwhelming. the "strictness" of php is actually beneficial for debugging.
let's break down your function and try to get it to function right.$value and $type from where are we getting these variables from?
what's going on here?

NetMonkey

Is crap. I read a post today from 2011 that says since WordPress 3.3 (current version now is 3.9), that the way custom user fields are called is different. The post > http://goo.gl/x5deHi says the way to call custom WP fields like 5 of mine are, is like this:

i suggest when naming global variables, functions, and/or classes that extend or are in tandem with scripts that are not your own to not use the naming conventions they use. this could cause naming conflicts down the road.

NetMonkey

Thanks ShrineDesigns, I see your logic after reading the post I read earlier today. I noticed how you started the array with the standard WP user fields and then called the custom fields. But what is the post string is expecting the fields in their original order? Do you feel they would be accepted in that order? That's my concern.

ShrineDesigns

order does not apply to data sent via HTTP POST or HTTP GET.

// post/get data string
"var1=val&var2=val&var3=val"
// is the same as
"var2=val&var3=val&var1=val"

NetMonkey

Thanks ShrineDesigns, now before I can test, I have to have a compatible post string. The original post string that posted the static data reads like this:

NetMonkey

ShrineDesigns, after swapping email with one of the developers over at WordPress.org support, I learned that the WP get_currentuserinfo() may not be able to query on custom fields in the WP DB, but that wp_get_current_user could.

With that in mind and using some of your code examples, I've come up with a hybrid version that's still not exactly correct syntax and wanted to run it by you. This is what I have that's throwing an - unexpected T_ARRAY error on my line that begins with - $custom_field =. Using the WP codex support suggestion, this is what my code looks like now:

I removed two fields from the WP user array and added them to the $query_vals array when I discovered I can't get the WP user password out of the DB in plain text because it's encrypted, so I'm going to pass a static value of the same password for all users to the receiving site and because the country will always be the US, I moved that field up also.

How is my syntax incorrect?

NetMonkey

ShrineDesigns, by making my declarations under the global for wp_get_current_user, now in the code for the custom mepr-fields, why couldn't I lose the if and elseif statements and just do the code like this:

multi-assignment is the similar to declaring a variable, but you do it with multiple variables (typical use-case: establish common default values)

<?php
$x = $y = 0; // both $x and $y have the value of 0
// a little more trickier but the same principle
$x = ((bool) $y = 0); // $x is cast to false and $y is 0
?>

i suggest when naming global variables, functions, and/or classes that extend or are in tandem with scripts that are not your own to not use the naming conventions they use. this could cause naming conflicts down the road.

PHP manual: user-defined functions
you know that word press declares get_currentuserinfo() but you define a function with the same name.
using global variables is good and all. but you still have to follow PHP's variable naming conventions. using dashes in a variable is not permitted:

NetMonkey

Thanks for the direction. My code has undergone several changes in the last three hours. I have the correct array format now to get user data, but my post string is choking because of my inability to use the right syntax. Here's what it looks like now:

I'm almost there I believe, but it's failing on the foreach ($data line. Thoughts?

NetMonkey

ShrineDesigns - I understand the elseif statements now, but I believe the format I put the array into now may be correct (minus needing the statements) because it came from the WP codex and the code doesn't choke with parse errors until it get to the post string.

So, is the answer to merge most of your code into the array format I'm using and change the post string?