• Hey all,

    I’m recoding an associative array that used to post 16 static data fields. I just need to get 10 WP current user fields and pass the field data to a post string.

    This php code allows user registration data to be passed to a third-party content provider website, so the WP user doesn’t have to go through the whole user signup process twice.

    I first need the correct coding for the get current user array and then define the correct values for the post string. The post string is expecting certain label names for each value and will not except WP label names.

    Example – post string expects ‘firstname’ => ‘value’, not ‘user_firstname’ => ‘value’. After days of fatal parse errors for syntax, now I just get a fail status. Either my array code or post code, (or both), is somehow flawed. This is what I have:

    global $current_user;
    Function get_currentuserinfo(){
    $current_user-> $label;
    $n="user_variable_{$type}_name";
    ${$n} = true;
    //Get values for these 10 user fields
    $query_vals = array(
    
        'user_firstname' . $current_user->firstname =>'n',
        'user_lastname' . $current_user->lastname =>'n',
        'mepr-address-one' . $current_user->address =>'n',
        'mepr-address-city' . $current_user->city =>'n',
        'mepr-address-state' . $current_user->state =>'n',
        'mepr-address-zip' . $current_user->zip =>'n',
        'mepr-address-country' . $current_user->country =>'n',
        'user_email' . $current_user->email =>'n',
        'user_login' . $current_user->username =>'n',
        'user_pass' . $current_user->password =>'n'
    
    );
    }
    // Generate the POST string
    // These 1st 3 lines did work when posting static data
    $postdata = '';
    foreach($query_vals as $key => $value) {
        $postdata .= $key.'='.urlencode($value).'&';
    
    //These last two lines aren't posting my get user data
    foreach($query_vals as $key => $value)
    	$postdata .= $key.'='.urlencode($n=$value).'&';
    }

    Php Wizards please help!

Viewing 11 replies - 31 through 41 (of 41 total)
  • Thread Starter NetMonkey

    (@headmonkey)

    Something in the code for the WP_vals array must be wrong. These two lines are lines 39 and 40.

    'username' => $current_user->data->user_login,
       'email' => $current_user->data->user_email,

    After checking the server error log it says:

    PHP Notice: Trying to get property of non-object in register_member.php on line 39

    PHP Notice: Trying to get property of non-object in register_member.php on line 40

    Do I need to recode them like the other lines for user info?

    Moderator bcworkz

    (@bcworkz)

    I teated your code on my installation, you don’t want any of the include lines except include ($location . '/wp-load.php');
    wp-load.php causes all of the other files to be loaded, including them again generates PHP warnings.

    The non-object error happens when you are not logged into WP because there is no current user in that case. Log in and try again, the error should go away.

    The correct query string for cURL will happen when you change $query_vals to $WP_vals in the foreach loop.

    With those corrections, cURL should be passed the correct fields. If you still get no response, confirm your server has cURL installed, not all do.

    Thread Starter NetMonkey

    (@headmonkey)

    I just changed to include line as suggested. What is confusing about the advise you just gave for the foreach loop is, how does the $query_vals array above this array that holds 8 fields of data, ever get passed if I change the code.

    Did you forget that there is another array above this one that has static data? cURL is installed on my server, because I ran this file with all static fields and values and tested it with a success result.

    Earlier today I saw that the WP_vals array is (semi-working). With all of the code for the fields as:

    $WP_vals['firstname'] = $current_user['first_name'][0];
    	$WP_vals['lastname'] = $current_user['last_name'][0];
    	$WP_vals['address'] = $current_user['mepr-address-one'][0];
    	$WP_vals['city'] = $current_user['mepr-address-city'][0];
    	$WP_vals['state'] = $current_user['mepr-address-state'][0];
    	$WP_vals['zip'] = $current_user['mepr-address-zip'][0];

    I received the most descriptive error message yet, which was:

    string(9) “firstname” NULL string(8) “lastname” NULL string(7) “address” NULL string(4) “city” NULL string(5) “state” NULL string(3) “zip” NULL Status: fail.

    Is this because of the zero’s at the end of each line?

    Thread Starter NetMonkey

    (@headmonkey)

    I saw some code on a site of a WP array that would seem to easily solve my post string code requirements, keeping them exactly as the other array’s are already set and I was wondering if this may be the best solution.

    Here’s the code that I saw and adapted:

    '$WP_vals' => array(
        array(
            // get user keys and values
            'keys' => 'firstname',
            'values' =>  = $current_user->'first_name',
            'keys' => 'lastname',
            'values' =>  = $current_user->'last_name',
            'keys' =>  'address',
            'values' =>  = $current_user->'mepr-address-one',
            'keys' => 'city',
            'values' =>  = $current_user->'mepr-address-city',
            'keys' => 'state',
            'values' =>  = $current_user->'mepr-address-state',
            'keys' => 'zip',
            'values' =>  = $current_user->'mepr-address-zip'
            ));

    Since the other post string is coded like this:

    $postdata = '';
    foreach($query_vals as $key => $value) {
    	$postdata .= $key.'='.urlencode($value).'&';
    }

    Wouldn’t this work and allow me to use the same post string code that I know works with the other array?

    Thread Starter NetMonkey

    (@headmonkey)

    bcworkz, this all has taken a bit to sink in and has been quite a learning experience. I’m extremely close to getting this right. Using your first array code (which is absolutely correct) and inserting the code you suggested to display the array information, was quite telling as a tool for troubleshooting.

    I believe there are three problems left. All of the data expected in both arrays, (minus) the user_login and user_email is being returned, which is returning NULL. The other two problems are, because there are two arrays, string numbers are being duplicated and last, I don’t believe the one post expected will allow for two arrays.

    So I need a different method for getting the user_login and user_email and it seems that doing an array merge will solve the other two problems.

    This is what the code returned for both arrays:

    All User Data:
    
    array(8) {
      ["api_username"]=>
      string(19) "api-name-here"
      ["api_password"]=>
      string(6) "password-here"
      ["api_key"]=>
      string(31) "key-here"
      ["password"]=>
      string(10) "password-here"
      ["country"]=>
      string(2) "US"
      ["perkalert"]=>
      int(0)
      ["offer_radius"]=>
      int(20)
      ["send_welcome_email"]=>
      int(1)
    }
    All User Data:
    
    NULL
    
    All User Data:
    
    array(6) {
      ["firstname"]=>
      string(3) "Bob"
      ["lastname"]=>
      string(6) "Tester"
      ["address"]=>
      string(16) "555 Anywhere St."
      ["city"]=>
      string(11) "Little Rock"
      ["state"]=>
      string(2) "AR"
      ["zip"]=>
      string(5) "72223"
    }

    Status: fail
    Where should I insert code to merge the arrays?

    Thread Starter NetMonkey

    (@headmonkey)

    I’ve been busy reading up on all of the different ways to merge arrays and have a workable solution now using this example:

    $static_query=array("a"=>"red","b"=>"green");
    $WP_vals=array("c"=>"blue","b"=>"yellow");
    
    $query_vals = $static_vals + $WP_vals;

    This works perfectly outputting all fields into one array. But, I don’t know the code I should use to get the user_login and user_email fields, as what was coded,
    ‘global $current_user;
    get_currentuserinfo();
    $WP_vals = array(
    ‘username’ => $current_user->data->user_login,
    ’email’ => $current_user->data->user_email,

    );`
    doesn’t return those fields. This is the last code needed now. Because of the array union, this also now allows using the original post string code. Any thoughts for code for the last two fields needed?

    Moderator bcworkz

    (@bcworkz)

    I’m glad you’ve got most of it worked out!

    I don’t know what to tell you about the login and email, the code works fine on my installation as long as someone is logged in, otherwise I do get the non-object errors.

    Maybe after get_currentuserinfo(); place var_dump('$current_user'); and post the resulting dump here.

    Thread Starter NetMonkey

    (@headmonkey)

    When I place var_dump(‘$current_user’); after get_currentuserinfo(); all it displays is – string(13) “$current_user”.

    But immediately below it, it’s displaying all of the other user array data which confirms I’m logged in, but not getting the two fields.

    Yesterday I reformatted the code for those two fields as a test, then those keys did appear in the array, but each value was NULL. I put back to code as it was and now get no key name or value returned.

    There must be some variable in our two installs that is differnt which would explain the results I’m seeing.

    Thread Starter NetMonkey

    (@headmonkey)

    bcworkz – Thank you for all of your help with this code. I just got it working. This has been quite an experience.

    Moderator bcworkz

    (@bcworkz)

    That’s great news! Congratulations!

    BTW, about my var_dump request… I’m such a dope sometimes! I don’t know why I put quotes in the code, it should have been var_dump( $current_user ); …Duh!
    No matter now, just so you can see that even experienced coders can do really stupid things sometimes 🙂

    Thread Starter NetMonkey

    (@headmonkey)

    That’s fine bcworkz, I got through it all with your help and I’m greatly appreciative. I’ve come to learn that php is unforgiving if not followed to the character, as (I’ve seen my share of syntax errors) doing this.

    But lucky for me, there’s people like you, ww.wp.xz.cn and other resources like php.net, wordpress.stackexchange.com and stackoverflow.com to help us get through our coding issues. Take care bro.

Viewing 11 replies - 31 through 41 (of 41 total)

The topic ‘Get current user array with post string’ is closed to new replies.