views:

186

answers:

2

Hey guys I am using open id authentication on my website and after authenticating I am getting a url from openid providers i.e yahoo and google

http://www.mysite.com/openid-login.php?
openid.identity=https://me.yahoo.com/a/1234567&
openid.ax.value.nickname=john&
[email protected]&


http://www.mysite.com/openid-login.php?
openid.identity=https://www.google.com/accounts/o8/1234567&
[email protected]&
openid.ext1.value.country=IN

I have trimmed the urls a bit for clarity. I would like to create a single function for both that can set the email(if exists), nickname(if exits), identity(openid ina ) in an array and return the values. eg.

function userdetails(array_get){
......
......
return $userdetails;
}
$userdetails =userdetails($_GET);

$userdetails['nickname'] would give me the nickname if exists and similarly for the email and identity. Thanks

A: 

I thought by "getting a url" you meant actually a URL. Accept the answer below me. :)

parse_url()
parse_str()

function userdetails( $url, $keep = array( 'email', 'nickname', 'identity' ) ) {
    $array = parse_str( parse_url( $url, PHP_URL_QUERY ) );

    $return = array();
    foreach ( $keep as $key ) {
        if ( isset( $array[ $key ] ) ) {
            $return[ $key ] = $array[$key];
        }
    }

    return $return;
}
William
+1  A: 

I did not create this function nor take credit for it. This was pulled and modified from the Simple OpenID library. If somebody has a link please post it in the comments as I don't have access to the original source.

/**
 * Method to filter through $_GET array for requested user information.
 * Will return an array of trimmed userinfo.
 */
public function filterUserInfo($arr_get) {
    $valid_ax_types = array('nickname' => 1, 'email' => 1, 'fullname' => 1, 'dob' => 1, 'gender' => 1, 'postcode' => 1, 'country' => 1, 'language' => 1, 'timezone' => 1, 'firstname' => 1, 'lastname' => 1);
    $userinfo = array();
    foreach ($arr_get as $key => $value) {
        // trim the key
        $trimmed_key = substr($key, strrpos($key, "_") + 1);

        // check for valid openid_ext1 values
        if (stristr($key, 'openid_ext1_value') && isset($value[1])) {
            $userinfo[$trimmed_key] = $value;
        }

        // check for valid openid_ax values
        if (stristr($key, 'openid_ax_value') && isset($value[1])) {
            $userinfo[$trimmed_key] = $value;
        }

        // check for valid sreg_ values
        else if (stristr($key, 'sreg_') && array_key_exists($trimmed_key, $arr_ax_types)) {
            $userinfo[$trimmed_key] = $value;
        }
    }
    return $userinfo;
}
cballou
It appears as though you are receiving periods instead of underscores. Simply make a few minor modifications to the above code to change **'openid_ext1_value'** and **'openid_ax_value'**
cballou