0

The WordPress Stackexchange said my post was off topic because it was a php syntax question and not a WordPress question so I'm posting here hoping for help.

After countless test runs to overcome fatal parse errors, I now have an error free php file, but it does nothing. The array values are not being passed correctly in an xml string the server will accept and I can't see any errors that tell me anything.

This file was originally written to run on an html website and for all of the array's fields to be manually entered into one array to test the connection and processing of the xml string.

The first six values of the array are known values that never change and can be hard coded, but the last 10 values are always different and must be queried to create the array.

I need to either pass data from two arrays into one single xml string, or somehow code one array that does the work.

I'm the first guy who is adapting this code to work in a WordPress environment. The mepr- fields are custom usermeta fields I need from the WordPress DB. All this file does is pass user registration information so the partner website has all of the same information the first site does, so a user doesn't have to signup twice for both sites.

This is my code which currently doesn't work, and doesn't report errors:

error_reporting(E_ERROR);
ini_set(‘display_errors’,true);

// Set the Query POST parameters
$query_vals = array(
'api_username' => 'set-value-username',
'api_password' => 'set-value-password', 
'api_key' => 'set-value-api-key',
'perkalert' => 0,
'offer_radius' => 20,
'send_welcome_email' => 1
);

// Insert pluggable.php before calling get_currentuserinfo()
require (ABSPATH . WPINC . '/pluggable.php');

// Get new WordPress user registration information
global $current_user;
get_currentuserinfo(); 
// Values in this array are variables
$query_vals == array(
'firstname: ' . $current_user==> 'user_firstname',
'lastname: ' . $current_user==> 'user_lastname',
'adress: ' . $current_user==> 'mepr-address-one',
'city: ' . $current_user==> 'mepr-address-city',
'state: ' . $current_user==> 'mepr-address-state',
'zip: ' . $current_user==> 'mepr-address-zip',
'country: ' . $current_user==> 'mepr-address-country',
'email: ' . $current_user==> 'user_email',
'username: ' . $current_user==> 'user_login',
'password: ' . $current_user==> 'user_pass'
);

// Generate the POST string
$postdata = '';
foreach($query_vals as $key => $value) {
$postdata .= $key.'='.urlencode($value).'&';
}

// Chop of the trailing ampersand
$postdata = rtrim($postdata, '&');

// create a new cURL resource
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://third-party-site.com/register_member.xml');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);

// Save response to a string
$response = curl_exec($ch);
curl_close($ch);

$xml = simplexml_load_string($response);

if ($xml === false) {
die('Error parsing XML');   
}

//var_dump($xml);
echo "Status: ".$xml->status;

The main focus is the two arrays and how to pass the data. If I get errors getting the WordPress user data, I'll take that up with the WordPress Stackexchange.

Any help here is greatly appreciated.

NetMonkey
  • 11
  • 1
  • 5
  • Just so it's clear, the other server receiving the xml post is expecting to see the labels - firstname, lastname, etc. and their values, but the field names in the WP DB may not be the same field names. So that needs to be considered in coding, but I don't know how to do it. – NetMonkey Apr 25 '14 at 21:15
  • on this line `$query_vals == array(` you create a non-associative array, that overwrites your initial associative array. I'll try to post an answer that explains it. – Joe T Apr 25 '14 at 21:19
  • 1. Set `error_reporting` to `E_ALL` – warnings, notices etc. might also point to relevant issues. 2. `ini_set(‘display_errors’,true);` – use _correct_ quote characters here, `'` or `"`. – CBroe Apr 25 '14 at 21:24
  • And what is the connection to `XML` here? Doesn’t look like you are _generating_ XML yourself here, but only the result you’ll get back from the API is XML – or will be, once you feed the correct parameters to it. So connection to `XML` == NULL. – CBroe Apr 25 '14 at 21:26
  • I just tweaked the code for the error reporting. Nice catch CBroe. – NetMonkey Apr 25 '14 at 21:45

1 Answers1

0

I think it'll depend on what the site your posting to expects, but is there a reason you can't combine all the fields into a single associative array?

So instead of this:

$query_vals == array(
'firstname: ' . $current_user==> 'user_firstname',
'lastname: ' . $current_user==> 'user_lastname',
'adress: ' . $current_user==> 'mepr-address-one',
'city: ' . $current_user==> 'mepr-address-city',
'state: ' . $current_user==> 'mepr-address-state',
'zip: ' . $current_user==> 'mepr-address-zip',
'country: ' . $current_user==> 'mepr-address-country',
'email: ' . $current_user==> 'user_email',
'username: ' . $current_user==> 'user_login',
'password: ' . $current_user==> 'user_pass'
);   

i think you need to have, for example, this:

$query_vals['firstname'] = $current_user==> 'user_firstname';  
$query_vals['lastname']  = $current_user==> 'user_lastname';  
$query_vals['adress']    = $current_user==> 'mepr-address-one';  
$query_vals['city']      = $current_user==> 'mepr-address-city';  
$query_vals['state']     = $current_user==> 'mepr-address-state';  
$query_vals['zip']       = $current_user==> 'mepr-address-zip';  
$query_vals['country']   = $current_user==> 'mepr-address-country';  
$query_vals['email']     = $current_user==> 'user_email';  
$query_vals['username']  = $current_user==> 'user_login';  
$query_vals['password']  = $current_user==> 'user_pass';  

Now you'd have a single associative array and your post may work, depending what the other side expects.

Joe T
  • 2,300
  • 1
  • 19
  • 31
  • Thanks Joe T. I'll test and repost results. – NetMonkey Apr 25 '14 at 21:29
  • I just tested and got a parse error - Parse error: syntax error, unexpected '>' in /includes/test_register_member2.php on line 24. Line 24 is the first line of the code you wrote. Look down through the code at the line that generates the post string where it defines the query vals. Does your code need a tweak? – NetMonkey Apr 25 '14 at 21:40
  • i copied what you had, probably need to change the format on every line there `$current_user['user_firstname'];` – Joe T Apr 25 '14 at 21:45
  • Joe T - I just stripped all of the second equals from each line and now got this error - unexpected T_DOUBLE_ARROW. – NetMonkey Apr 25 '14 at 21:50
  • was it working before? just use normal array notation, should work: `$current_user['user_lastname'];` – Joe T Apr 25 '14 at 21:52
  • I may be getting closer now. I'm seeing a WP error that's complaining about how I'm calling pluggable.php. I have to resolve that first. I know it has to be called before I can call get_currentuserinfo. – NetMonkey Apr 25 '14 at 22:23
  • I made some code changes and am getting closer. Now WP is not throwing errors about the way I'm calling pluggable.php, it's complaining about the way I have the array code to get user info. It's throwing this error > Undefined variable: current_user and the last error line reads > Status: fail which means I'm close and the code for the get user array has to be changed it seems. – NetMonkey Apr 25 '14 at 22:51
  • I got WP to stop throwing errors by changing all of the get user array code to this - 'firstname: ' . $current_user= 'user_firstname', now it gives me a non descript error by just saying - Status: fail. – NetMonkey Apr 25 '14 at 23:07
  • This is now looking like my fail error may be because of the way the code for the post string is written. Not cool. – NetMonkey Apr 25 '14 at 23:11
  • What other code could I insert for better error reporting so I could see where it's failing? – NetMonkey Apr 25 '14 at 23:27
  • you could echo the $postdata to make sure you've set everything as expected. Looks like you might be missing the CURLOPT_POST, take a look at http://stackoverflow.com/questions/3080146/post-data-to-url-php – Joe T Apr 26 '14 at 02:01
  • This file was written originally with the $query_vals array and had 16 static label values with 16 static user data fields. But because I need the last 10 user fields to be variables, the challenge is to 1. - make the correct call to the WordPress usermeta DB table and get the current user_x, user_x, etc. for the array, 2. set some values that the string understands like 'firstname' = 'user_firstname' and then 3., I believe a few lines of new post string code has to be added to handle the new variable data. But I haven't been able to wrap my head around all of the variables yet. – NetMonkey Apr 27 '14 at 19:56
  • I've been scouring the internet reading, coding and testing over and over. Today may be a breakthrough. I just read a post from 2011 that said since WP 3.3 things changed for the way WP custom usermeta DB fields are called, like my custom fields that start with mepr-field. The post is here > (http://goo.gl/x5deHi) so it seems some of my code calling the custom user mepr-fields has to have that code. I'm just unsure how to code it. – NetMonkey Apr 30 '14 at 17:03
  • maybe try something and create a new question around that – Joe T May 01 '14 at 18:21