Search code examples
phphybridauth

How to update the user status when using hybridauth in PHP?


I am trying to connect with social web sites like facebook,twitter,linkedin etc using Hybridauth This is what I do to connect with social web sites. I am new to PHP just started to learn and doing this. 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>

So please help me how to go about this?, Here I do display user information like first name and last name and picture url. I want to add another text box which will enable to update user status or post wall.


Solution

  • From the User Guide: Update User Status

    This method exists:
    $adapter->setUserStatus("");

    Note From User Guide:
    Currently only Facebook, Twitter, Identica, LinkedIn, QQ, Sina, Murmur, Pixnet and Plurk support this feature

    You could create a form on authenticated_user.php and add a text field with ID "status_update". When it's submitted, you could check for it and run the setUserStatus method.

    // 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