Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

2 arrays with simple xml post string failing

General discussions related to php

Moderators: macek, egami, gesf

2 arrays with simple xml post string failing

Postby NetMonkey » Tue Apr 29, 2014 12:20 pm

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 guys, thanks.
NetMonkey
New php-forum User
New php-forum User
 
Posts: 1
Joined: Tue Apr 29, 2014 11:41 am

Re: 2 arrays with simple xml post string failing

Postby seandisanti » Tue Apr 29, 2014 10:23 pm

you start running into syntax errors around this line:
$value = '&'

That is probably the one the error is about. Whenever you see 'unexpected ________' that usually means you've either forgotten a semicolon like this one, or you've improperly terminated a string or left it open.
Then there are more issues below
$result = array_merge($query_vals, $current_user_info); {

it shouldn't end with a brace like that. Your next line starts a foreach loop, and that one should end with a brace but it doesn't, nor does the foreach that starts immediately after it. You're on the right track.

I didn't throw it into an editor or anything just eyeballed it in notepad and those were the ones that jumped out at me. If you use an intelligent IDE, it should help you avoid some of those with syntax highlighting and autocompletion. I personally like Aptana Studio 3, Netbeans, and even notepad++ in that order, but they each have their own strengths and weaknesses.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.