在PHP中使用hybridauth时如何更新用户状态?

时间:2012-07-04 12:06:18

标签: php hybridauth

我正在尝试使用Hybridauth与Facebook,Twitter,linkedin等社交网站建立联系 这就是我与社交网站建立联系的方式。我是PHP新手,刚刚开始学习并做到这一点。 的的index.php

<?php
    # start a new PHP session
    session_start();

    // we need to know it
    $CURRENT_URL = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
      //  print "CURRENT_URL $CURRENT_URL";
    // change the following paths if necessary 
    $config   = dirname(__FILE__) . '/hybridauth/config.php';
    require_once( "hybridauth/Hybrid/Auth.php" );

    try{
        $hybridauth = new Hybrid_Auth( $config );
    }
    catch( Exception $e ){
        echo "Ooophs, we got an error: " . $e->getMessage();
    }

    $provider = ""; 

    // handle logout request
    if( isset( $_GET["logout"] ) ){
        $provider = $_GET["logout"];

        $adapter = $hybridauth->getAdapter( $provider );

                $adapter->logout();

        header( "Location: index.php"  );

        die();
    }

    // if the user select a provider and authenticate with it 
    // then the widget will return this provider name in "connected_with" argument 
    elseif( isset( $_GET["connected_with"] ) && $hybridauth->isConnectedWith( $_GET["connected_with"] ) ){
        $provider = $_GET["connected_with"];

        $adapter = $hybridauth->getAdapter( $provider );

        $user_data = $adapter->getUserProfile();

        // include authenticated user view
        include "authenticated_user.php";

        die();
    } // if user connected to the selected provider 

    // if not, include unauthenticated user view
    include "unauthenticated_user.php";

authenticated_user.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en">
<head>
    <style type="text/css" media="screen">
        <!--
            BODY {
                margin: 10px;
                padding: 0;
            }
            H1 {
                margin-bottom: 2px;
                font-family: Garamond, "Times New Roman", Times, Serif;
            }
            FIELDSET {
                border: 1px solid #ccc;
                padding: 1em;
                margin: 0;
            }
            LEGEND {
                color: #666666;
            } 
        -->
    </style> 
</head>
<body>
    <br /><br />
    <center>
        <h1>Welcome <?php echo $user_data->displayName; ?></h1>
        <br />
        <br /> 

        <table width="600" border="0" cellpadding="2" cellspacing="2">
          <tr>
            <td valign="top"><fieldset>
                <legend>Connected user badge</legend>
                <table width="100%" border="0" cellpadding="2" cellspacing="2"> 
                  <tr> 
                    <td width="100" rowspan="5">
                        <?php
                            if( $user_data->photoURL ){
                        ?>
                            <a href="<?php echo $user_data->profileURL; ?>"><img src="<?php echo $user_data->photoURL; ?>" title="<?php echo $user_data->displayName; ?>" border="0" width="100" height="120"></a>
                        <?php
                            }
                            else{
                        ?> 
                            <img src="avatar.png" title="<?php echo $user_data->displayName; ?>" border="0" >
                        <?php
                            } 
                        ?>
                    </td>
                    <td width="60" nowrap><div align="right"><strong>Provider</strong></div></td>
                    <td align="left" ><?php echo $adapter->id; ?></td>
                  </tr> 
                  <tr>
                    <td nowrap><div align="right"><strong>Identifier</strong></div></td>
                    <td align="left"><?php echo $user_data->identifier; ?></td>
                  </tr>
                  <tr>
                    <td nowrap><div align="right"><strong>Dispaly name</strong></div></td>
                    <td align="left"><?php echo $user_data->displayName; ?></td>
                  </tr>
                  <tr>
                    <td nowrap><div align="right"><strong>Email</strong></div></td>
                    <td align="left"><?php 
                            if( $user_data->email ){
                                echo $user_data->email ; 
                            }
                            else{
                                echo "not provided by " . $adapter->id ; 
                            }
                        ?></td>
                  </tr>
                  <tr>
                    <td nowrap valign="top"><div align="right"><strong>Profile URL</strong></div></td>
                    <td align="left" valign="top"><?php echo $user_data->profileURL; ?></td>
                  </tr>
                                    <tr>
                                        <td align="right"><strong>Update Status</strong></td>
                                        <td align="center"><input name="updatestatus" id="updatestatus" /></td>
                  </tr>
                </table> 

              </fieldset></td>
          </tr>
          <tr>
            <td valign="top" align="right">
                <img src="arrow.gif" align="texttop" style="margin-top:-5px;" >
                <a href="?logout=<?php echo $adapter->id; ?>">Log me out</a>
            </td>
          </tr>
        </table>
    </center> 
</body>
</html>


unathenticated.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en">
<head>
    <style type="text/css" media="screen">
        <!--
            BODY {
                margin: 10px;
                padding: 0;
            }
            H1 {
                margin-bottom: 2px;
                font-family: Garamond, "Times New Roman", Times, Serif;
            }
            FIELDSET {
                border: 1px solid #ccc;
                padding: 1em;
                margin: 0;
            }
            LEGEND {
                color: #666666;
            }
            INPUT {
                display: block;
                font-family: Arial, verdana;
                padding: 7px;
                border: 1px solid #999;
                margin: 10px;
            } 
   /* Gradient 2 */
   .textbox{
    background-image: url(images/bg.png);
    border: 1px solid #6297BC;
    width: 230px;
}
        -->
    </style> 
</head>
<body>
    <br /><br />
    <center>
        <h1>Social web sites integration</h1>
        <br />
        <br /> 
        <table width="380" border="0" cellpadding="2" cellspacing="2">
                    <tr>
            <td valign="top"><fieldset>
                <legend>Sign-in form</legend>
                <table width="00%" border="0" cellpadding="2" cellspacing="2">
                  <tr>
                    <td><div align="right"><strong>login</strong></div></td>
                                        <td><input type="text" name="textfield" id="textfield" class="textbox"/></td>
                  </tr>
                  <tr>
                    <td><div align="right"><strong>password</strong></div></td>
                    <td><input type="text" name="textfield2" id="textfield2" class="textbox"/></td>
                  </tr>
                  <tr>
                    <td>&nbsp;</td>
                    <td><div align="center">
                        <input type="submit" value="Sign-in" />
                      </div></td>
                  </tr>
                </table>
              </fieldset></td>
          </tr>
          <tr>
            <td valign="top" align="right">
                <!--img src="arrow.gif" align="texttop" style="margin-top:-5px;" -->
                <!-- CODE REQUIRED BY THE WIDGET -->
                    <link media="screen" rel="stylesheet" href="widget/css/colorbox.css" />
                    <script src="widget/js/jquery.min.js"></script>
                    <script src="widget/js/jquery.colorbox.js"></script> 
                    <script>
                        $(document).ready(function(){
                            $(".widget_link").colorbox({iframe:true, innerWidth:430, innerHeight:222});
                        }); 
                    </script>
                <!-- /WIDGET -->
                <!-- 
                    LINK TO THE WIDGET 
                        return_to: call back this page after authenticatin the user
                        ts: nocache
                --> 
                    <a href="widget/?_ts=<?php echo time(); ?>&return_to=<?php echo urlencode( $CURRENT_URL ); ?>" class='widget_link' title="Socail web sites linker">Or sign-in with Social web sites</a>
            </td>
          </tr>
        </table> 
        <br /><br /><br />
        <!--b>Note:</b> This is just a proof of concept! it works good enough to try out on Firefox or on Chrome.-->
    </center> 
</body>
</html>

那么请帮我解决这个问题?在这里,我会显示用户信息,如名字和姓氏以及图片网址。我想添加另一个文本框,可以更新用户状态或发布墙。

1 个答案:

答案 0 :(得分:3)

来自用户指南:Update User Status

此方法存在:
$adapter->setUserStatus("");

用户指南中的注释:
目前只有Facebook,Twitter,Identica,LinkedIn,QQ,新浪,Murmur,Pixnet和Plurk支持此功能

您可以在authenticated_user.php上创建表单,并添加ID为“status_update”的文本字段。提交后,您可以检查它并运行setUserStatus方法。

// if the user select a provider and authenticate with it
    // then the widget will return this provider name in "connected_with" argument
    elseif( isset( $_GET["connected_with"] ) && $hybridauth->isConnectedWith( $_GET["connected_with"] ) ){
    $provider = $_GET["connected_with"];

    $adapter = $hybridauth->getAdapter( $provider );


    // *****************************************************
    // NEW CODE TO UPDATE USER STATUS
    if( isset( $_GET["status_update"] ) && !empty( $_GET["status_update"] ) ) {
      $adapter->setUserStatus( $_GET["status_update"] );
    }


    $user_data = $adapter->getUserProfile();

    // include authenticated user view
    include "authenticated_user.php";

    die();
} // if user connected to the selected provider