0

The guys over at the other Stackoverflow said I should post this here. I've been reading and tweaking code for three days and can't figure this out, because I'm new to php. The developer that wrote this php login script to a remote server didn't code it for WordPress and not for an automatic login like I need. I think I've figured out the automatic login at the end of the code I didn't post here.

He created this code for his array that will have set values for the three api strings that never change, but I need to include the logged in WordPress user's username and password to be included in his array. The array info is passed by xml later in the script. This is for an automated SSL login to another server and when an image is clicked it runs the php code.

This is the code I just got from him, but I don't believe it's correct because all of the colors just changed in Notepad++ when I inserted it:

// Set the Query POST parameters - array    
$query_vals = array(
'api_username' => 'api-username-goes-here',
'api_password' => 'password-here', 
'api_key' => 'api-key-here’,
'username' => $current_user[‘user_login’],
'password' => $current_user[‘user_pass’]        
);

And the suggestions I got from the guys at Stackoverflow said:

You need to call this somewhere in your code first:

global $current_user;
get_currentuserinfo();

Then you'll want to change your array to stuff like this:

'username' => $current_user->user_login

and then someone posted a corrected code sample of what I posted with this:

// Set the Query POST parameters
$query_vals = array(
    'api_username' => 'api-username-goes-here',
    'api_password' => 'password-here', 
    'api_key' => 'api-key-here',
    'username' => $current_user['user_login'],
    'password' => $current_user['user_pass']
);

So now I'm really confused what to do, because I don't what to screw up the developer's array. So I guess my question is how to adapt the code passing the three api strings that don't change and the WP currently logged in users username and password?

I am working with a file copy so the original code doesn't get trashed.

This is the whole php file being called by an onclick event:

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

// Set the Query POST parameters
$query_vals = array(
'api_username' => 'website-api-username',
'api_password' => 'api-password', 
'api_key' => 'api-key goes here',
'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 off the trailing ampersand
$postdata = rtrim($postdata, '&');

// create a new cURL resource
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://third-party-provider.com/encrypt_login.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);

// post the user data
$xml"&encrypted_username=".$encrypted_username"&encrypted_password=".$encrypted_password';
if ($xml = FALSE)
$url = ('https://website-that-is-originating-the-array.com/error')
;

// Save response to a string
//var_dump($xml);
echo "Status: ".$xml->status;

That's the entire php file except for the closing tag.

  • Add the code you are trying to use, broken or not, as a unified block so we can see how it all goes together. – s_ha_dum Apr 22 '14 at 19:56
  • 1
    This is a PHP syntax question, not specific to WordPress. The problem is an incorrect character (a curly brace) closing the 'api-key-here' string. Note 'api-key-here' vs 'api-key-here’. – Chip Bennett Apr 22 '14 at 19:57
  • Good call on the incorrect character. – NetMonkey Apr 22 '14 at 20:09
  • 1
    Except, make that a curly quote not a curly brace. Derp. – Chip Bennett Apr 22 '14 at 20:17
0

Here is some code; you need to see that the $current_user variable is actually an object and not an array ( USE $current_user->user_login AND NOT $current_user['user_login'] ).

I also added a simple check to make sure the user is logged in.

// Set the Query POST parameters - array    
$query_vals = array(
'api_username' => 'api-username-goes-here',
'api_password' => 'password-here', 
'api_key' => 'api-key-here'
);


//if( is_user_logged_in() ):

global $current_user;
get_currentuserinfo();

$query_vals['username'] = $current_user->user_login;
$query_vals['password'] = $current_user->user_pass;

//else:

//wp_die('You must be logged in');

//endif;
  • Normally I would agree that the added code is needed. But the image that is coded with the onclick event is inside a member only area, so all users will be logged in before they ever see the image. – NetMonkey Apr 22 '14 at 20:29
  • Douglas, do you still think I have to add the code you mentioned if the user is already logged in? – NetMonkey Apr 22 '14 at 20:53
  • If you are in a members only area, then you probably do not need it. But unless you have a high traffic site, It will not hurt anything. – Douglas.Sesar Apr 22 '14 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.