www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 29

Thread: 2 arrays with simplexml load string failing

Hybrid View

  1. #1
    Join Date
    Apr 2014
    Location
    Little Rock, AR
    Posts
    24

    Question 2 arrays with simplexml load string failing

    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();

    Here's my code:

    PHP Code:
        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($chCURLOPT_URL'https://secureserver/client/register_member.xml');
        
    curl_setopt($chCURLOPT_HEADERfalse);
        
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
        
    curl_setopt($chCURLOPT_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.

  2. #2
    Join Date
    Feb 2014
    Location
    Canada
    Posts
    155
    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:

    PHP Code:
    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).'&';

  3. #3
    Join Date
    Apr 2014
    Location
    Little Rock, AR
    Posts
    24

    Exclamation

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

    PHP Code:
    // Call WordPress function wp_get_current_user_info()
    //Create array

    function wp_get_current_user() {
        
    $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,
                    
    '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

    ); }

    $postdata '';
    $current_user_info wp_get_current_user();
    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) {
           
         }
    }

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

    // create a new cURL resource
    $ch curl_init();
    curl_setopt($chCURLOPT_URL'https://secureserver/register_member.xml');
    curl_setopt($chCURLOPT_HEADERfalse);
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
    curl_setopt($chCURLOPT_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

  4. #4
    Join Date
    Apr 2014
    Location
    Little Rock, AR
    Posts
    24
    After many code edits, this is my code now that's throwing a server 500 error starting at my line 16:

    PHP Code:
    // Call WordPress function wp_get_current_user_info()
    //Create array

    function wp_get_current_user() {
        
    $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,
                    
    '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

    ); }

    $postdata '';
    $current_user_info wp_get_current_user();
    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) {
           
         }
    }

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

    // create a new cURL resource
    $ch curl_init();
    curl_setopt($chCURLOPT_URL'https://secureserver/register_member.xml');
    curl_setopt($chCURLOPT_HEADERfalse);
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
    curl_setopt($chCURLOPT_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

  5. #5
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    PHP Code:
    function wp_get_current_user()
    {
        
    $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,
            
    '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
        
    );

    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.

  6. #6
    Join Date
    Apr 2014
    Location
    Little Rock, AR
    Posts
    24

    Question Another developr's suggestion

    Quote Originally Posted by ShrineDesigns View Post
    PHP Code:
    function wp_get_current_user()
    {
        
    $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,
            
    '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
        
    );

    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.

  7. #7
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    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?
    PHP Code:
           $current_user-> $value;
           
    $n="user_variable_{$type}_name";
           ${
    $n} = true
    in php (as well as most languages) "-" is an operator. you'll need to either verify that this is in fact the key/property naming convention used in the returned value from wp_get_current_user_info()
    PHP Code:
    $test = (object) array('foo-bar' => 10);
    $x $test->foo-bar// should produce an error
    $x $test->{'foo-bar'}; // will work as intended 

  8. #8
    Join Date
    Apr 2014
    Location
    Little Rock, AR
    Posts
    24
    ShrineDesigns - I've come to think now that the code you mentioned:

    PHP Code:
           $current_user-> $value;
           
    $n="user_variable_{$type}_name";
           ${
    $n} = true
    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:

    PHP Code:
    if ( $current_user->has_prop'my-field' ) ) )
        echo 
    '<p>
      ' 
    $current_user->get'my-field' ) . '
    </p>'

    So it seems my array might need another function and mixed code. And when using an array and generating a POST string, it should look something like this:

    PHP Code:
    foreach ( get_user_meta$user->ID ) as $key => $values ) {
        
    var_dump$key$values );

    But, php newb here, so I'm not sure how to edit the code to fit those parameters for my array. In my first array that is coded like this:

    PHP Code:
    // 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' => 

        
    ); 
    Is it possible to just recode that to include the WP functions and user data from the other array and then just have one array?

  9. #9
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    after a bit of research i came up with this. based on your first post, it queries the user data present in word press and returns an array
    PHP Code:
    function myext_get_user_data()
    {
        
    $user_id get_current_user_id();
        
    $user get_userdata($user_id);
        
    $data = array(
            
    'firstname' => $data['user_firstname'],
            
    'lastname' => $data['user_lastname'],
            
    'email' => $data['user_email'],
            
    'username' => $data['user_login'],
            
    'password' => $data['user_pass']
        );

        
    // address
        
    if (array_key_exists('mepr-address-one'$user))
            
    $data['address'] = $user['mepr-address-one'];
        elseif (
    array_key_exists('mepraddressone'$user))
            
    $data['address'] = $user['mepraddressone'];
        
    // city
        
    if (array_key_exists('mepr-address-city'$user))
            
    $data['city'] = $user['mepr-address-city'];
        elseif (
    array_key_exists('mepraddresscity'$user))
            
    $data['city'] = $user['mepraddresscity'];
        
    // state
        
    if (array_key_exists('mepr-address-state'$user))
            
    $data['state'] = $user['mepr-address-state'];
        elseif (
    array_key_exists('mepraddressstate'$user))
            
    $data['state'] = $user['mepraddressstate'];
        
    // zip
        
    if (array_key_exists('mepr-address-zip'$user))
            
    $data['zip'] = $user['mepr-address-zip'];
        elseif (
    array_key_exists('mepraddresszip'$user))
            
    $data['zip'] = $user['mepraddresszip'];
        
    // country
        
    if (array_key_exists('mepr-address-country'$user))
            
    $data['country'] = $user['mepr-address-country'];
        elseif (
    array_key_exists('mepraddresscountry'$user))
            
    $data['country'] = $user['mepraddresscountry'];
        return 
    $data;

    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.

  10. #10
    Join Date
    Apr 2014
    Location
    Little Rock, AR
    Posts
    24
    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.

  11. #11
    Join Date
    Apr 2014
    Location
    Little Rock, AR
    Posts
    24
    If the simplexml post string is expecting to only see $key and $value when doing $postdata and urlencode, it seems this data has to be appended using $key and $value. Or?

  12. #12
    Join Date
    Apr 2014
    Location
    Little Rock, AR
    Posts
    24
    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:

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

    And to accept both the query_vals data and this array's data, a new post string has to be defined that's compatible. What are your thoughts on this?

    PHP Code:
    $postdata '';
    $data get_user_data();
    $result array_merge($query_vals$data);
    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 ( get_userdata$user->ID ) as $keys => $values ) {
        
    var_dump$keys$values );
    }
           

    Am I close here?

  13. #13
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    order does not apply to data sent via HTTP POST or HTTP GET.
    Code:
    // post/get data string
    "var1=val&var2=val&var3=val"
    // is the same as
    "var2=val&var3=val&var1=val"

  14. #14
    Join Date
    Apr 2014
    Location
    Little Rock, AR
    Posts
    24
    ShrineDesigns, what does this look like for post string code? Close?

    PHP Code:
    // Generate the POST string
    $postdata '';
    $data get_user_data();
    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 ($data$user_id ) as $keys => $values )) 
        
    var_dump$keys$values );
        
    $postdata .= $key.'='.urlencode($value).'&';
        
    $postdata .= $keys.'='.urlencode($values).'&';


  15. #15
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    typically when i map an array to get/post var string I do
    PHP Code:
    <?php
    $array 
    = array(
        
    'var1' => 'value',
        
    'var2' => 'value',
        
    'var3' => 'value',
    );
    $retval $s '';

    foreach (
    $array as $k => $v) {
        
    $retval .= $s urlencode($k) . '=' urlencode($v);
        
    $s '&';
    }
    echo 
    $retval// "var1=value&var2=value&var3=value"
    ?>
    this prevents adding an unnecessary "&" at the end of the string (which may cause problems).
    In your case, you can do
    PHP Code:
    //...
        
    $postdata .= $s urlencode($key) . '=' urlencode($value)
                . 
    '&' urlencode($keys) . '=' urlencode($values);
        
    $s '&';
    // ... 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles