PHP OpenID中AX的示例用法

时间:2009-07-26 05:11:41

标签: php openid janrain

我正在使用JanRain的PHP OpenID库。它附带了使用SReg扩展的示例脚本。但我希望它可以与谷歌合作(实际上它适用于auth),但Google使用AX(属性交换)代替SReg来获取其他数据。出于某种原因,JanRain的库在示例脚本中缺少AX支持,并且AX脚本中的代码注释不在我的理解中,尽管SReg脚本中的注释清楚为1-2-3。

有没有人知道如何在没有太多痛苦的情况下实施AX?

2 个答案:

答案 0 :(得分:46)

陷入同样的​​问题。 AX.php的一些挖掘让我有了一个良好的开端。没有找到任何错误,也没有超过基本测试,也没有与谷歌以外的任何人测试过。这不是很好:需要错误处理等等。但这应该让你开始。如果我有健全的东西,我会发布更新...

先抛出......     

//  oid_request.php

// Just tested this with/for Google, needs trying with others ...
$oid_identifier = 'https://www.google.com/accounts/o8/id';

// Includes required files
require_once "Auth/OpenID/Consumer.php";
require_once "Auth/OpenID/FileStore.php";
require_once "Auth/OpenID/AX.php";

// Starts session (needed for YADIS)
session_start();

// Create file storage area for OpenID data
$store = new Auth_OpenID_FileStore('./oid_store');

// Create OpenID consumer
$consumer = new Auth_OpenID_Consumer($store);

// Create an authentication request to the OpenID provider
$auth = $consumer->begin($oid_identifier);

// Create attribute request object
// See http://code.google.com/apis/accounts/docs/OpenID.html#Parameters for parameters
// Usage: make($type_uri, $count=1, $required=false, $alias=null)
$attribute[] = Auth_OpenID_AX_AttrInfo::make('http://axschema.org/contact/email',2,1, 'email');
$attribute[] = Auth_OpenID_AX_AttrInfo::make('http://axschema.org/namePerson/first',1,1, 'firstname');
$attribute[] = Auth_OpenID_AX_AttrInfo::make('http://axschema.org/namePerson/last',1,1, 'lastname');

// Create AX fetch request
$ax = new Auth_OpenID_AX_FetchRequest;

// Add attributes to AX fetch request
foreach($attribute as $attr){
    $ax->add($attr);
}

// Add AX fetch request to authentication request
$auth->addExtension($ax);

// Redirect to OpenID provider for authentication
$url = $auth->redirectURL('http://localhost:4001', 'http://localhost:4001/oid_catch.php');
header('Location: ' . $url);

...然后抓住

<?php

//  oid_catch.php

// Includes required files
require_once "Auth/OpenID/Consumer.php";
require_once "Auth/OpenID/FileStore.php";
require_once "Auth/OpenID/AX.php";

// Starts session (needed for YADIS)
session_start();

// Create file storage area for OpenID data
$store = new Auth_OpenID_FileStore('./oid_store');

// Create OpenID consumer
$consumer = new Auth_OpenID_Consumer($store);

// Create an authentication request to the OpenID provider
$auth = $consumer->complete('http://localhost:4001/oid_catch.php');

if ($response->status == Auth_OpenID_SUCCESS) {
    // Get registration informations
    $ax = new Auth_OpenID_AX_FetchResponse();
    $obj = $ax->fromSuccessResponse($response);

    // Print me raw
    echo '<pre>';
    print_r($obj->data);
    echo '</pre>';
    exit;


} else {
  // Failed
}

那些应该是基础......

答案 1 :(得分:4)

请求一半正在运行,但是我在Catch中遇到了失败。

上面的行

$auth = $consumer->complete('http://localhost:4001/oid_catch.php');

$response = $consumer->complete('http://localhost:4001/oid_catch.php');

否则,响应对象来自哪里? 我没有在我的回复中返回openid.current_url来检查网址?