2 arrays with simplexml load string failing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
NetMonkey
Forum Newbie
Posts: 3
Joined: Tue Apr 29, 2014 3:34 pm

2 arrays with simplexml load string failing

Post by 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 and I'm getting a syntax error, unexpected T_VARIABLE on line 42:
//$query_vals = array();

Here's my code:

Code: Select all

    error_reporting(E_ALL);
    ini_set('display_errors',true);

    // Set the Query POST parameters
    $query_vals = array(
        'api_username' => 'api-name-here',
        'api_password' => 'api-password',
        'api_key' => 'key-goes-here',
       'perkalert' => 0,
       'offer_radius' => 20,
       'send_welcome_email' => 1

    );

    // Call WordPress function wp_get_current_user_info()
    //Create array
    function wp_get_current_user_info() {
        $current_user = wp_get_current_user();
       $current_user-> $value;
       $n="user_variable_{$type}_name";
       ${$n} = true;
        $current_user_info = array(
                'firstname' . $current_user->user_firstname =>'n',
                'lastname' .  $current_user->user_lastname =>'n',
                'address' . $current_user->mepr-address-one =>'n',
                'city' . $current_user->mepr-address-city =>'n',
                'state' . $current_user->mepr-address-state =>'n',
           'zip' . $current_user->mepr-address-zip =>'n',
           'country' . $current_user->mepr-address-country =>'n',
           'email' . $current_user->user_email =>'n',
           'username' . $current_user->user_login =>'n',
           'password' . $current_user->user_pass =>'n'

                       );
        return $current_user_info;
    }

    $postdata = '';
    $key = '=';
    $value = '&'
    $query_vals = array();
    $current_user_info = array();
    $result = array_merge($query_vals, $current_user_info); {
    foreach($query_vals as $key => $value);
    foreach($current_user_info 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://secureserver/client/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);

    //var_dump($xml);
    echo "Status: ".$xml->status;
Any help is really appreciated. Thanks.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: 2 arrays with simplexml load string failing

Post by Celauran »

Missing semi-colon on the line above
NetMonkey
Forum Newbie
Posts: 3
Joined: Tue Apr 29, 2014 3:34 pm

Re: 2 arrays with simplexml load string failing

Post by NetMonkey »

Thanks Celauran, but after inserting the semi-colon, my code is still failing.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: 2 arrays with simplexml load string failing

Post by Celauran »

With what error this time?

What's going on here? This definitely won't work. foreach starts a loop. You need to do something inside that loop.

Code: Select all

foreach($query_vals as $key => $value);
    foreach($current_user_info as $key => $value);
NetMonkey
Forum Newbie
Posts: 3
Joined: Tue Apr 29, 2014 3:34 pm

Re: 2 arrays with simplexml load string failing

Post by NetMonkey »

Celauran - The only error code I'm getting isn't helpful at all. It's only - Status: Fail. The reason that code is in there is, it's a change to the original code that (was):
foreach($query_vals as $key => $value) {
$postdata .= $key.'='.urlencode($value).'&';
}
When it was processing the one static array with the 16 static fields.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: 2 arrays with simplexml load string failing

Post by Celauran »

Code: Select all

	$current_user_info = array(
		'firstname' . $current_user->user_firstname =>'n',
		'lastname' .  $current_user->user_lastname =>'n',
		'address' . $current_user->mepr-address-one =>'n',
		'city' . $current_user->mepr-address-city =>'n',
		'state' . $current_user->mepr-address-state =>'n',
		'zip' . $current_user->mepr-address-zip =>'n',
		'country' . $current_user->mepr-address-country =>'n',
		'email' . $current_user->user_email =>'n',
		'username' . $current_user->user_login =>'n',
		'password' . $current_user->user_pass =>'n'
	);
This whole bit here doesn't make sense. Variable names cannot contain dashes, so mepr- is definitely wrong. Even if that were right, you'd be assigning the value 'n' to a bunch of changing array keys. What is this function supposed to do?

My best guess is that you'd want something like this

Code: Select all

	$current_user_info = array(
		'firstname' => $current_user->user_firstname,
		'lastname' =>  $current_user->user_lastname,
		'address' => $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
	);
But it's just that; a guess.

Your foreach would then perhaps look like this

Code: Select all

foreach($query_vals as $key => $value) {
	$postdata .= $key.'='.urlencode($value).'&';
}
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: 2 arrays with simplexml load string failing

Post by Celauran »

Code: Select all

$key = '=';
$value = '&';
$query_vals = array();
$current_user_info = array();
$result = array_merge($query_vals, $current_user_info);
foreach($query_vals as $key => $value) {
	$postdata .= $key.'='.urlencode($value).'&';
}
This doesn't make sense either, though. You're merging two empty arrays, trying to iterate over the result (which is also an empty array), and, if it weren't empty, creating a string of ==%26&==%26&==%26& etc.

Maybe replace all of that with

Code: Select all

$postdata = '';
$current_user_info = wp_get_current_user_info();
foreach($query_vals as $key => $value) {
	$postdata .= $key.'='.urlencode($value).'&';
}
Again, though, still guessing.
Post Reply