2011-12-14 30 views
0

Facebook集成已在我的網站上完美工作了一段時間,然後在facebook上發生了一些事情,因爲它現在失敗了。Facebook集成突然失敗,使用這個PHP類

有人可以看看我在我的網站上使用的代碼,並建議我應該做些什麼才能儘快恢復這項工作,而無需對整個實施進行改造?

<?php 
// http://developers.facebook.com/docs/reference/fql/user 

class Facebook_class 
{ 

var $cookie; 

function Facebook_class() { 
    $this->cookie = $this->get_facebook_cookie(FACEBOOK_APP_ID, FACEBOOK_SECRET); 
} 

function getUserid() { 
    $cookie = $this->getCookie(); 
    $fb_userid = $cookie['uid']; 
    return $fb_userid; 
} 

function getProfilePicture() { 
    $url = 'https://graph.facebook.com/'.$this->getUserid().'/picture?type=large'; 
    //$url = 'api.facebook.com/method/fql.query?query=SELECT pic_big FROM user WHERE uid = '.$this->getUserid(); 
    $url = $this->get_redirect_url($url); 
    return $url; 
} 

function getUserData() { 
    if($this->getCookie()) { 
     $url = 'https://graph.facebook.com/me?access_token='.$this->getAccessToken(); 
     $userData = json_decode(file_get_contents($url)); 
     return $userData; 
    } 
} 

function getCookie() { 
    return $this->cookie; 
} 

function getAccessToken() { 
    return $this->cookie['access_token']; 
} 

function loadJsSDK($path_to_library='') { 
    echo '<script type="text/javascript"> 
    //<![CDATA[ '; 

    ?> 
    function logoutFacebookUser(){FB.logout(function(response){window.location.reload();});} 
    function fbActionConnect(){FB.login(function(response){if (response.session){window.location = "http://www.mysite.com/signin/fbconnect";if(response.perms){}else{}}else{}}, {perms:'publish_stream,email'});} 
    function fbAppActionConnect(){FB.login(function(response){if (response.session){window.location = "http://www.mysite.com/signin/fbappconnect";if(response.perms){}else{}}else{}}, {perms:'publish_stream,email'});} 
    function fbLinkActionConnect(){FB.login(function(response){if (response.session){window.location = "http://www.mysite.com/index.php?name=signin&file=MyServices&op=linkacc";if(response.perms){}else {}}else{}},{perms:'publish_stream,email'});} 
    function fbActionCartConnect(id, sport) {FB.login(function(response){if(response.session){window.location = "//index.php?name=signin&file=cart&id=" + id + "&sport=" + sport + "&op=fbsignup";if (response.perms){}else{}}else{}},{perms:'publish_stream,email'});} 
    window.fbAsyncInit = function() {FB.init({appId: xxxxxxxxxxxxxxxxx, channelUrl:'http://www.mysite.com/channel.html', status: true, cookie: true, xfbml: true});};(function() {var e = document.createElement('script'); e.async = true;e.src = document.location.protocol +'//connect.facebook.net/en_US/all.js';document.getElementById('fb-root').appendChild(e);}());//]]></script> 
    <?php 
} 

function get_facebook_cookie($app_id, $application_secret) { 
    $args = array(); 
    parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args); 
    ksort($args); 
    $payload = ''; 
    foreach ($args as $key => $value) { 
    if ($key != 'sig') { 
     $payload .= $key . '=' . $value; 
    } 
    } 
    if (md5($payload . $application_secret) != $args['sig']) { 
    return null; 
    } 
    return $args; 
} 

function get_redirect_url($url) { 
    $redirect_url = null; 

    $url_parts = @parse_url($url); 
    if (!$url_parts) return false; 
    if (!isset($url_parts['host'])) return false; //can't process relative URLs 
    if (!isset($url_parts['path'])) $url_parts['path'] = '/'; 

    $sock = fsockopen($url_parts['host'], (isset($url_parts['port']) ? (int)$url_parts['port'] : 80), $errno, $errstr, 30); 
    if (!$sock) return false; 

    $request = "HEAD " . $url_parts['path'] . (isset($url_parts['query']) ? '?'.$url_parts['query'] : '') . " HTTP/1.1\r\n"; 
    $request .= 'Host: ' . $url_parts['host'] . "\r\n"; 
    $request .= "Connection: Close\r\n\r\n"; 
    fwrite($sock, $request); 
    $response = ''; 
    while(!feof($sock)) $response .= fread($sock, 8192); 
    fclose($sock); 

    if (preg_match('/^Location: (.+?)$/m', $response, $matches)){ 
     if (substr($matches[1], 0, 1) == "/") 
      return $url_parts['scheme'] . "://" . $url_parts['host'] . trim($matches[1]); 
     else 
      return trim($matches[1]); 

    } else { 
     return false; 
    } 
} 

function getFacebookFriends($criteria='') { 
    $name = $criteria['name']; 

    if($name=='') $name = 'me'; 

    $url = 'https://graph.facebook.com/'.$name.'/friends?access_token='.$this->getAccessToken(); 
    $content = @file_get_contents($url,0,null,null); 
    $content = json_decode($content,true); 

    $users = $this->formatFacebookUsers($content); 

    return $users; 
} 

function formatFacebookUsers($content) { 
    for($i=0; $i<count($content['data']); $i++) { 
     $id = $content['data'][$i]['id']; 
     $name = $content['data'][$i]['name']; 

     $picture = 'https://graph.facebook.com/'.$id.'/picture?type=square'; //square, small, large 
     $url = 'http://www.facebook.com/profile.php?id='.$id; 

     $users[$i]['id'] = $id; 
     $users[$i]['name'] = $name; 
     $users[$i]['picture'] = $picture; 
     $users[$i]['url'] = $url; 
    } 
    return $users; 
} 

function getFacebookAccounts() { 
    $url = 'https://graph.facebook.com/me/accounts?access_token='.$this->getAccessToken(); 
    $content = @file_get_contents($url,0,null,null); 
    $content = json_decode($content,true); 
    return $content; 
} 

function displayUsersIcons($criteria) { 
    $users = $criteria['users']; 
    $nb_display = $criteria['nb_display']; 
    $width = $criteria['width']; 

    if($width=='') $width="30"; 

    if($nb_display>count($users) || $nb_display=='') $nb_display=count($users); //display value never bigger than nb users 

    $display = ''; 
    for($i=0;$i<$nb_display;$i++) { 
     $name = $users[$i]['name']; 
     $picture = $users[$i]['picture']; 
     $url = $users[$i]['url']; 

     $display .= '<a href="'.$url.'" target="_blank" title="'.$name.'">'; 
     $display .= '<img src="'.$picture.'" width="'.$width.'" style="padding:2px;">'; 
     $display .= '</a>'; 
    } 
    return $display; 
} 

function getFacebookFeeds() { 

    $url = 'https://graph.facebook.com/me/posts?access_token='.$this->getAccessToken(); 

    $ch = curl_init(); 
    $timeout = 5; 
    curl_setopt($ch,CURLOPT_URL,$url); 
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); 
    $data = curl_exec($ch); 
    curl_close($ch); 

    $data = json_decode($data,true); 
    $dataList = $this->formatFacebookPosts($data); 

    return $dataList; 
} 

function formatFacebookPosts($data) { 
    $i=0; 
    foreach($data['data'] as $value) { 
     $id = $value['id']; 
     $from_id = $value['from']['id']; 
     $from_name = $value['from']['name']; 

     $type = $value['type']; //video, link, status, picture, swf 
     $message = $value['message']; 
     $picture = $value['picture']; 
     $link = $value['link']; 
     $source = $value['source']; //for videos 
     $name = $value['name']; //for videos or links 
     $caption = $value['caption']; //for videos (domain name url) or links 
     $description = $value['description']; //for videos 
     $icon = $value['icon']; 
     $created = $value['created_time']; 
     $likes_nb = $value['likes']; 

     $comments = $value['comments']['data']; //(message, created_time) 
     $comments_nb = $value['comments']['count']; 
     $action_comment = $value['actions'][0]['link']; 

     $picture_url = 'https://graph.facebook.com/'.$from_id.'/picture'; 
     $profile_url = 'http://www.facebook.com/profile.php?id='.$from_id; 

     $attribution = $value['attribution']; 

     if($type=='status') { 
      $dataList[$i]['id'] = $id; 
      $dataList[$i]['from_id'] = $from_id; 
      $dataList[$i]['from_name'] = $from_name; 
      $dataList[$i]['type'] = $type; 
      $dataList[$i]['message'] = $message; 
      $dataList[$i]['picture'] = $picture; 
      $dataList[$i]['link'] = $link; 
      $dataList[$i]['source'] = $source; 
      $dataList[$i]['name'] = $name; 
      $dataList[$i]['caption'] = $caption; 
      $dataList[$i]['description'] = $description; 
      $dataList[$i]['icon'] = $icon; 
      $dataList[$i]['created'] = $created; 
      $dataList[$i]['attribution'] = $attribution; 
      $dataList[$i]['likes_nb'] = $likes_nb; 
      $dataList[$i]['comments'] = $comments; 
      $dataList[$i]['comments_nb'] = $comments_nb; 
      $dataList[$i]['action_comment'] = $action_comment; 
      $dataList[$i]['picture_url'] = $picture_url; 
      $dataList[$i]['profile_url'] = $profile_url; 
      $i++; 
     } 
    } 
    return $dataList; 
} 

function updateFacebookStatus($status) { 
    $postParms = "access_token=".$this->getAccessToken()."&message=".$status; 
    $ch = curl_init('https://graph.facebook.com/me/feed'); 

    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_HEADER, false); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postParms); 
    $results = curl_exec($ch); 
    curl_close($ch); 
} 

function postmsg() { 
    $FILE_PATH = $_SERVER["DOCUMENT_ROOT"]."images/default/webedition1.jpg"; 
    $token=$this->getAccessToken(); 

    if (file_exists($FILE_PATH)) { 

     $args = array('message' => 'From the coaches locker'); 
    $args['image'] = '@' . realpath($FILE_PATH); 


    $arr_attachment = array('image' => '@'.realpath($FILE_PATH), 
          'message' => 'Test caption' 
         ); 
    $_curl = curl_init(); 
    curl_setopt($_curl, CURLOPT_URL, "https://graph.facebook.com/me/photos?access_token=".$token); 
    curl_setopt($_curl, CURLOPT_HEADER, false); 
    curl_setopt($_curl, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($_curl, CURLOPT_POST, true); 
    curl_setopt($_curl, CURLOPT_POSTFIELDS, $arr_attachment); 
    curl_setopt($_curl, CURLOPT_SSL_VERIFYHOST, 0); 
    curl_setopt($_curl, CURLOPT_SSL_VERIFYPEER, 0); 

    $_photo = curl_exec($_curl); 
    echo($_photo); 

    } else { 
    echo "cannot find file:".$FILE_PATH; 
    } 
} 


} 

?> 

謝謝。

+0

需要知道您所看到的錯誤。至少,你期望什麼,以及你得到了什麼。 – 2011-12-14 18:40:49

+0

當然,隔夜fbActionConnect()函數停止彈出facebook登錄窗口。所以沒有人現在可以註冊或驗證我的網站了。這一切工作正常,沒有任何代碼更改,它只是在一夜之間停止工作。 – 2011-12-14 18:44:15

回答

0

的Facebook引入了OAuth2認證昨天一些重大的變動對JavaScript SDK:http://developers.facebook.com/blog/post/614/

更多細節:http://developers.facebook.com/blog/post/525/

基本上一些變化,我看到的都是:

  1. FB.getSession( )現在更改爲FB.getAuthResponse()
  2. FB.init()現在具有'誓言'值,如同'true'一樣。
  3. '燙髮' 現在在登錄按鈕的HTML
  4. 可能FB.Event.subscribe( 'auth.sessionChange' ..)現在FB.Event.subscribe( 'auth.authResponseChange' 更改爲 '範圍'。 )

希望有所幫助。