2014-02-12 21 views
2

我正在使用Zend我想從Facebook導入聯繫人,我爲此使用了一個插件,它與gmail完美配合。當我嘗試從Facebook導入聯繫人時,它會完美顯示我的個人資料,但空的聯繫人列表...我如何解決此問題? 對不起太多的代碼...如何使用Zend從Facebook導入聯繫人

<?php 
    $config = 'hybridauth/hybridauth/config.php'; 

    require_once("hybridauth/hybridauth/Hybrid/Auth.php");  
    $error = "";   
    if (isset($_GET["error"])) {  
     $error = '<b style="color:red">' . trim(strip_tags($_GET["error"])) . '</b><br /><br />'; 
    }    
    if (isset($_GET["provider"]) && $_GET["provider"]):  
     try {  

      $hybridauth = new Hybrid_Auth($config);   

      $provider = @ trim(strip_tags($_GET["provider"]));   

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

      $hybridauth->redirect("../message/profile?provider=$provider"); 
     } catch (Exception $e) {  
      // In case we have errors 6 or 7, then we have to use Hybrid_Provider_Adapter::logout() to 

      switch ($e->getCode()) {  
       case 0 : $error = "Unspecified error."; 
        break;  
       case 1 : $error = "Hybriauth configuration error."; 
        break;  
       case 2 : $error = "Provider not properly configured."; 
        break; 
       case 3 : $error = "Unknown or disabled provider."; 
        break;  
       case 4 : $error = "Missing provider application credentials."; 
        break;  
       case 5 : $error = "Authentication failed. "; 
         $error .= "The user has canceled the authentication "; 
         $error .= "or the provider refused the connection."; 
        break;  
       case 6 : $error = "User profile request failed. "; 
         $error .= "Most likely the user is not connected to the "; 
         $error .= "provider and he should to authenticate again."; 
        $adapter->logout();  
        break;  
       case 7 : $error = "User not connected to the provider.";  
        $adapter->logout();  
        break; 
      }    
      $error .= "<br /><br /><b>Original error message:</b> " . $e->getMessage();  
      $error .= "<hr /><pre>Trace:<br />" . $e->getTraceAsString() . "</pre>"; 
     }  
    endif; 
    ?> 
    <div class="page-header"> 
     <h2> 
     Import Contacts 
     </h2> 
     </div> 
    <div>  
    <?php 

    if ($error) {  
     echo '<p><h3 style="color:red">Error!</h3>' . $error . '</p>';  
     echo "<pre>Session:<br />" . print_r($_SESSION, true) . "</pre><hr />"; 
    } 
    ?>  
    <div class="inviteFriends"> 
     <table width="500" border="0" cellpadding="2" cellspacing="2">  
       <tr>  
        <td align="left" valign="top">  
         <fieldset>  
          <legend>Sign-in with one of these providers</legend>  
           <a class="socialIcon" href="?provider=Facebook"><img src="<?php echo $this->baseUrl() ?>/images/fb.png" width="181" height="60" border="0" alt="" /></a>   
           <a class="socialIcon" href="?provider=Google"> <img src="<?php echo $this->baseUrl() ?>/images/gmail.png" width="181" height="60" border="0" alt="" /> </a>  
           <a class="socialIcon" href="?provider=Twitter"><img src="<?php echo $this->baseUrl() ?>/images/tw.png" width="181" height="60" border="0" alt="" /></a>     
           <a class="socialIcon" href="?provider=LinkedIn"><img src="<?php echo $this->baseUrl() ?>/images/ln.png" width="181" height="60" border="0" alt="" /></a>  
         </fieldset>   
        </td>  
    <?php 
    try {  
     $hybridauth = new Hybrid_Auth($config);    
     $connected_adapters_list = $hybridauth->getConnectedProviders();   

     if (count($connected_adapters_list)) {?> 
    <?php 
     } 
    } catch (Exception $e) { 

     echo "Ooophs, we got an error: " . $e->getMessage();    
     echo " Error code: " . $e->getCode();    
     echo "<br /><br />Please try again.";    
     echo "<hr /><h3>Trace</h3> <pre>" . $e->getTraceAsString() . "</pre>"; 
    } 
    ?>  
       </tr>  
     </table> 
    </div>   
    </div> 

這是查找聯繫人代碼

<?php 
     $config = 'hybridauth/hybridauth/config.php'; 

     require_once("hybridauth/hybridauth/Hybrid/Auth.php"); 
     $hybridauth = new Hybrid_Auth($config); 
     $provider = @ trim(strip_tags($_GET["provider"])); 
     if (!$hybridauth->isConnectedWith($provider)) { 
     header("Location: importContacts?error=Your are not connected to $provider or your session has expired"); 
     } 
     ?> 
     <script type="text/javascript"> 
     function frmChk() { 
       var str = ''; 
     //  $('#frmContacts').submit(function(){ 
       $('input[id ^="chk_"]').each(function() { 
        var id = parseInt(this.id.replace('chk_', '')); 
        if ($('#chk_' + id).is(':checked')) { 
         str += (str.length == 0) ? id : ',' + id; 
        } 
       }); 
     //  alert(str); 
       $('#cnt_contacts').val(str); 
     //  return false; 
     //  }); 
       if (str.length > 0) { 
        return true; 
       } else { 
        return false; 
       } 
      } 
     </script> 
     <form name="frmContacts" id="frmContacts" method="post" 
action="inviteContacts" onsubmit="return frmChk();" > 
      <table width="90%" border="0" cellpadding="2" cellspacing="2"> 
       <tr> 
        <td valign="top"> 

     <?php echo $this->render('menu.phtml'); ?> 
         <fieldset> 
          <legend>My Contact list</legend> 
          <table width="100%"> 

     <?php 
     try { 
      $adapter = $hybridauth->getAdapter($provider); 
      $user_contacts = $adapter->getUserContacts(); 
      $i_c = 1; 
      foreach ($user_contacts as $item) { 
       ?> 
             <tr> 
              <td> 
<input type="checkbox" name="chk_<?php echo $i_c ?>" id="chk_<?php echo $i_c ?>" value="<?php echo $item->identifier; ?>" /> 
        </td> 
       <td align="left" valign="top" width="55" > 
       <?php 
       if ($item->photoURL) { 
        ?> 
       <a href="<?php echo $item->profileURL; ?>"><img src="<?php echo $item->photoURL; ?>" border="0" width="48" height="48"></a> 
                <?php 
               } else { 
                ?> 

     <img src="../images/avatar.png" width="48" height="48" > 
        <?php 
       } 
       ?>  </td> 
       <td align="left"> 
       <a href="<?php echo $item->profileURL; ?>"><b> 
     <?php echo $item->displayName; ?></b></a> 
<small>(ID:<?php echo $item->identifier; ?>)</small> 
     <br /><?php echo $item->description; ?> 
     <br /><small><?php echo $item->profileURL; ?></small> 
        <br /><hr /> 
       </td> 
      </tr> 
       <?php 
       $i_c++; 
      } 
      if (!count($user_contacts)) { 
       echo "No contact found!"; 
      } 
     } catch (Exception $e) { 
      if ($e->getCode() == 8) {   
       echo "Provider does not support this feature. Currently only <b>"; 
       echo "Facebook, MySpace, Twitter and LinkedIn</b> do support this!"; 
       echo "<br />Please refer to the user guide to know more";  
       echo "about each adapters capabilities."; 
       echo "<a href='http://hybridauth.sourceforge.net/userguide.html'>"; 
       echo "http://hybridauth.sourceforge.net/userguide.html</a>"; 
      } else { 

       echo "Well, got an error: " . $e->getMessage(); 
      } 
     } 
     ?>     </table> 
         </fieldset> 
        </td> 
        <td valign="top" width="250" align="left"> 
        </td> 
       </tr> 
       <tr> 
     <td colspan="2"><input type="submit" value="Invite" name="btnInvite" /> </td> 
       </tr> 
        </table> 
      <input type="hidden" name="cnt_contacts" id="cnt_contacts" value="" /> 
     </form> 
+0

您的代碼在哪裏? – Raptor

+0

我編輯了我的問題,現在我的importContacts文件的代碼 –

+0

您可以使用[CloudSponge.com](http://cloudsponge.com)。它支持PHP和幾個通訊錄,包括Gmail,Outlook,Yahoo,AOL,Linkedin和Facebook(這一個爲測試版)。 –

回答

0

您可以使用開放式邀請庫:

http://openinviter.com/download.php

開放邀請者是一個免費的庫來導入聯繫人。

OpenInviterTM是完全免費的開源解決方案