6

मुझे फेसबुक के साथ अपनी साइट में लॉगिन करने में समस्याएं हैं।PHP के लिए फेसबुक एसडीके का उपयोग कर फेसबुक के साथ लॉगिन करें

मैंने डेवलपर.facebook.com पर ट्यूटोरियल का पालन करके फेसबुक एसडीके का उपयोग करके लॉगिन बनाया है, लेकिन यह मेरे लिए काम नहीं करता है।

मैंने यह जानने के लिए एक सरल परीक्षण पृष्ठ लिखा है, क्या गलत है।

<!DOCTYPE html> 
<html> 
<head></head> 
<body> 
<pre> 
<?php 

$app_id   = '446458238729594'; 
$app_secret  = '********'; 
$redirect_uri = 'http://mysite.localhost/'; 

try { 

    echo('Including "facebook.php"...'); 
    require './src/facebook.php'; 
    echo("done.\n\n"); 

    echo('Starting session...'); 
    $result = session_start(); 
    if($result) { 
     echo("done.\n\n"); 
     echo("\n=====>>>>> Session array:\n" . var_export($_SESSION, true) 
       . "\n\n"); 
    } else { 
     echo("fail.\n\n"); 
     echo("\n=====>>>>> Session array:\n" . var_export($_SESSION, true) 
       . "\n\n"); 
    } 

    echo("Trying to get counter from the session...\n"); 
    if(isset($_SESSION['counter'])) { 
     echo('Value: ' . $_SESSION['counter'] . "\n"); 
     echo("Increasing to one...\n"); 
     $_SESSION['counter']++; 
     echo("done.\n"); 
     echo('Value: ' . $_SESSION['counter'] . "\n\n"); 
    } else { 
     echo("fail.\n"); 
     echo("Trying to add a counter and set it's value to 0...\n"); 
     $_SESSION['counter'] = 0; 
     echo('Value: ' . $_SESSION['counter'] . "\n"); 
     echo("done.\n\n"); 
    } 

    echo('Creating an instance of Facebook class...'); 
    $facebook = new Facebook(
     array(
      'appId'  => $app_id, 
      'secret' => $app_secret, 
     ) 
    ); 
    echo("done.\n\n"); 
    echo("The instance of Facebook class:\n" . str_replace($app_secret, 
      '>>>APP_SECRET<<<', var_export($facebook, true)) . "\n\n"); 

    echo("\n=====>>>>> Session array:\n" . var_export($_SESSION, true) 
      . "\n\n"); 

    echo('Trying to get user ID...'); 
    $user_id = $facebook->getUser(); 
    echo("done.\n\n"); 
    echo("User ID:\n" . var_export($user_id, true) . "\n\n"); 

    echo("\n=====>>>>> Session array:\n" . var_export($_SESSION, true) 
      . "\n\n"); 

    echo('Trying to get user profile info...'); 
    try { 
     $user_profile = $facebook->api('/me'); 
     echo("done.\n\n"); 
     echo("User profile info:\n" . var_export($user_profile, true) 
       . "\n\n"); 
    } catch(Exception $exception) { 
     echo("fail. Probably user is not logged in.\n\n"); 
     echo("Exception:\n--------\n" . str_replace($app_secret, 
       '>>>APP_SECRET<<<', var_export($exception, true)) 
       . "\n--------\n\n"); 
     $user_id = null; 
     echo("User ID is now NULL.\n\n"); 
    } 

    echo("\n=====>>>>> Session array:\n" . var_export($_SESSION, true) 
      . "\n\n"); 

    if($user) { 
     echo('Seems like user is logged in. Getting logout url...'); 
     $url = $facebook->getLogoutUrl(); 
     echo("done.\n\n"); 
    } else { 
     echo('Seems like user is NOT logged in. Getting login url...'); 
     $url = $facebook->getLoginUrl(
      array(
       'scope'   => 'read_stream, publish_stream, user_birthday,' 
         . ' user_location, user_work_history, user_hometown,' 
         . ' user_photos', 
       'redirect_uri' => $redirect_uri, 
      ) 
     ); 
     echo("done.\n\n"); 
    } 
    echo('URL:<br></pre><a href=' . $url .">Login/Logout</a><pre>\n\n"); 

    echo("\n=====>>>>> Session array:\n" 
      . var_export($_SESSION, true) . "\n\n"); 

    if($user) { 
     echo('Seems like user is still logged in. Trying to get some profile' 
       . ' info...'); 
     echo("\nCreating request...\n"); 
     $queries = array(
      array(
       'method'  => 'GET', 
       'relative_url' => '/' . $user, 
      ), 
      array(
       'method'  => 'GET', 
       'relative_url' => '/' . $user . '/home?limit=50', 
      ), 
      array(
       'method'  => 'GET', 
       'relative_url' => '/' . $user . '/friends', 
      ), 
      array(
       'method'  => 'GET', 
       'relative_url' => '/' . $user . '/photos?limit=6', 
      ), 
     ); 
     echo("Request:\n\n" . var_export($queries, true) . "\n\n"); 
     echo("\nEncoding request using JSON format...\n"); 
     $queries_encoded = json_encode($queries); 
     echo("Encoded request:\n\n" . var_export($queries_encoded, true) 
       . "\n\n"); 
     try { 
      echo("\nTrying to get response...\n"); 
      $response = $facebook->api('?batch=' . $queries_encoded, 'POST'); 
      echo("Response:\n\n" . var_export($response, true) . "\n\n"); 
      echo("\nTrying to decode response...\n"); 
      echo("\n" . json_decode($response[0]['body'], true) . "\n"); 
      echo("\n" . json_decode($response[1]['body'], true) . "\n"); 
      echo("\n" . json_decode($response[2]['body'], true) . "\n"); 
      echo("\n" . json_decode($response[3]['body'], true) . "\n"); 
      echo("\n\ndone.\n\n"); 
     } catch(Exception $exception) { 
      echo("fail.\n\n"); 
      echo("Exception:\n--------\n\n" . str_replace($app_secret, 
        '>>>APP_SECRET<<<', var_export($exception, true)) 
        . "\n--------\n\n"); 
     } 
    } else { 
     echo('Seems like user is still NOT logged in. At now we can\'t do' 
       . ' anything. Try to login using the URL above.'); 
    } 

    echo("\n\n========\n\nSession array:\n" . var_export($_SESSION, true) 
      . "\n\n"); 

} catch(Exception $exception) { 
    echo("\n\n\nAn exception have been trown:\n--------\n\n" . str_replace(
      $app_secret, '>>>APP_SECRET<<<', var_export($exception, true)) 
      . "\n--------\n\n"); 
    echo("\n\n========\n\nSession array:\n" . var_export($_SESSION, true) 
      . "\n\n"); 
} 

?> 
</pre> 
</body> 
</html> 

इस पेज के पहली यात्रा के बाद (मैं फेसबुक पर लॉग इन नहीं कर रहा हूँ), मैं इस आउटपुट प्राप्त:

Including "facebook.php"...done. 

Starting session...done. 


=====>>>>> Session array: 
array (
) 

Trying to get counter from the session... 
fail. 
Trying to add a counter and set it's value to 0... 
Value: 0 
done. 

Creating an instance of Facebook class...done. 

The instance of Facebook class: 
Facebook::__set_state(array(
    'sharedSessionID' => NULL, 
    'appId' => '446458238729594', 
    'appSecret' => '>>>APP_SECRET<<<', 
    'user' => NULL, 
    'signedRequest' => NULL, 
    'state' => NULL, 
    'accessToken' => NULL, 
    'fileUploadSupport' => false, 
    'trustForwarded' => false, 
)) 


=====>>>>> Session array: 
array (
    'counter' => 0, 
) 

Trying to get user ID...done. 

User ID: 
0 


=====>>>>> Session array: 
array (
    'counter' => 0, 
) 

Trying to get user profile info...fail. Probably user is not logged in. 

Exception: 
-------- 
FacebookApiException::__set_state(array(
    'result' => 
    array (
    'error_code' => 7, 
    'error' => 
    array (
     'message' => 'Failed to connect to 2a03:2880:2050:1f01:face:b00c:0:2: Network is unreachable', 
     'type' => 'CurlException', 
    ), 
), 
    'message' => 'Failed to connect to 2a03:2880:2050:1f01:face:b00c:0:2: Network is unreachable', 
    'string' => '', 
    'code' => 7, 
    'file' => '/srv/www/htdocs/mysite/web/src/base_facebook.php', 
    'line' => 967, 
    'trace' => 
    array (
    0 => 
    array (
     'file' => '/srv/www/htdocs/mysite/web/src/base_facebook.php', 
     'line' => 899, 
     'function' => 'makeRequest', 
     'class' => 'BaseFacebook', 
     'type' => '->', 
     'args' => 
     array (
     0 => 'https://graph.facebook.com/me', 
     1 => 
     array (
      'method' => 'GET', 
      'access_token' => '446458238729594|>>>APP_SECRET<<<', 
     ), 
    ), 
    ), 
    1 => 
    array (
     'file' => '/srv/www/htdocs/mysite/web/src/base_facebook.php', 
     'line' => 866, 
     'function' => '_oauthRequest', 
     'class' => 'BaseFacebook', 
     'type' => '->', 
     'args' => 
     array (
     0 => 'https://graph.facebook.com/me', 
     1 => 
     array (
      'method' => 'GET', 
     ), 
    ), 
    ), 
    2 => 
    array (
     'function' => '_graph', 
     'class' => 'BaseFacebook', 
     'type' => '->', 
     'args' => 
     array (
     0 => '/me', 
    ), 
    ), 
    3 => 
    array (
     'file' => '/srv/www/htdocs/mysite/web/src/base_facebook.php', 
     'line' => 644, 
     'function' => 'call_user_func_array', 
     'args' => 
     array (
     0 => 
     array (
      0 => 
      Facebook::__set_state(array(
      'sharedSessionID' => NULL, 
      'appId' => '446458238729594', 
      'appSecret' => '>>>APP_SECRET<<<', 
      'user' => 0, 
      'signedRequest' => NULL, 
      'state' => NULL, 
      'accessToken' => '446458238729594|>>>APP_SECRET<<<', 
      'fileUploadSupport' => false, 
      'trustForwarded' => false, 
     )), 
      1 => '_graph', 
     ), 
     1 => 
     array (
      0 => '/me', 
     ), 
    ), 
    ), 
    4 => 
    array (
     'file' => '/srv/www/htdocs/mysite/web/index.php', 
     'line' => 69, 
     'function' => 'api', 
     'class' => 'BaseFacebook', 
     'type' => '->', 
     'args' => 
     array (
     0 => '/me', 
    ), 
    ), 
), 
    'previous' => NULL, 
)) 
-------- 

User ID is now NULL. 


=====>>>>> Session array: 
array (
    'counter' => 0, 
) 

Seems like user is NOT logged in. Getting login url...done. 

URL: 
Login/Logout 


=====>>>>> Session array: 
array (
    'counter' => 0, 
    'fb_446458238729594_state' => '84260edcd60940884d261812496a488c', 
) 

Seems like user is still NOT logged in. At now we can't do anything. Try to login using the URL above. 

======== 

Session array: 
array (
    'counter' => 0, 
    'fb_446458238729594_state' => '84260edcd60940884d261812496a488c', 
) 

तब मैं दिए गए URL का उपयोग कर लॉगिन करने की कोशिश इस कोड है । यह मुझे मेरे फेसबुक ऐप प्राधिकरण पृष्ठ पर ले जाता है। अनुरोध की गई अनुमति की पुष्टि के बाद, यह मुझे इस URL पर रीडायरेक्ट:

http://mysite.localhost/?state=84260edcd60940884d261812496a488c&code=AQDkHPlXXweEiTjXg-sUXwwQAy0_xRYc89Opfz6AF9dlGOomCSG7fjf0440ctHuADKMEG4P7CheeNx9PnwUta-jkfpm03MjDCKyieOZpIPG-evlKYm64mRxD2Q5f_-HJROIC9I_-lHswr5RT3huSQySA55pD28b07Ouv87NqihZ1brGfU-_0LyhcdldtNikb-2xn6NRpa17xEmU37pBqDV1r#_=_ 

कि मैं उम्मीद करते हैं कि मैं में लॉग इन कर रहा हूँ के बाद और मेरे पेज प्राप्त करता है और मुझे मेरे फेसबुक प्रोफाइल से कुछ जानकारी से पता चलता है, लेकिन ऐसा नहीं है। मुझे यह आउटपुट मिलता है:

Including "facebook.php"...done. 

Starting session...done. 


=====>>>>> Session array: 
array (
    'counter' => 0, 
    'fb_446458238729594_state' => '84260edcd60940884d261812496a488c', 
) 

Trying to get counter from the session... 
Value: 0 
Increasing to one... 
done. 
Value: 1 

Creating an instance of Facebook class...done. 

The instance of Facebook class: 
Facebook::__set_state(array(
    'sharedSessionID' => NULL, 
    'appId' => '446458238729594', 
    'appSecret' => '>>>APP_SECRET<<<', 
    'user' => NULL, 
    'signedRequest' => NULL, 
    'state' => '84260edcd60940884d261812496a488c', 
    'accessToken' => NULL, 
    'fileUploadSupport' => false, 
    'trustForwarded' => false, 
)) 


=====>>>>> Session array: 
array (
    'counter' => 1, 
    'fb_446458238729594_state' => '84260edcd60940884d261812496a488c', 
) 

Trying to get user ID...done. 

User ID: 
0 


=====>>>>> Session array: 
array (
    'counter' => 1, 
) 

Trying to get user profile info...fail. Probably user is not logged in. 

Exception: 
-------- 
FacebookApiException::__set_state(array(
    'result' => 
    array (
    'error_code' => 7, 
    'error' => 
    array (
     'message' => 'Failed to connect to 2a03:2880:2050:1f01:face:b00c:0:2: Network is unreachable', 
     'type' => 'CurlException', 
    ), 
), 
    'message' => 'Failed to connect to 2a03:2880:2050:1f01:face:b00c:0:2: Network is unreachable', 
    'string' => '', 
    'code' => 7, 
    'file' => '/srv/www/htdocs/mysite/web/src/base_facebook.php', 
    'line' => 967, 
    'trace' => 
    array (
    0 => 
    array (
     'file' => '/srv/www/htdocs/mysite/web/src/base_facebook.php', 
     'line' => 899, 
     'function' => 'makeRequest', 
     'class' => 'BaseFacebook', 
     'type' => '->', 
     'args' => 
     array (
     0 => 'https://graph.facebook.com/me', 
     1 => 
     array (
      'method' => 'GET', 
      'access_token' => '446458238729594|>>>APP_SECRET<<<', 
     ), 
    ), 
    ), 
    1 => 
    array (
     'file' => '/srv/www/htdocs/mysite/web/src/base_facebook.php', 
     'line' => 866, 
     'function' => '_oauthRequest', 
     'class' => 'BaseFacebook', 
     'type' => '->', 
     'args' => 
     array (
     0 => 'https://graph.facebook.com/me', 
     1 => 
     array (
      'method' => 'GET', 
     ), 
    ), 
    ), 
    2 => 
    array (
     'function' => '_graph', 
     'class' => 'BaseFacebook', 
     'type' => '->', 
     'args' => 
     array (
     0 => '/me', 
    ), 
    ), 
    3 => 
    array (
     'file' => '/srv/www/htdocs/mysite/web/src/base_facebook.php', 
     'line' => 644, 
     'function' => 'call_user_func_array', 
     'args' => 
     array (
     0 => 
     array (
      0 => 
      Facebook::__set_state(array(
      'sharedSessionID' => NULL, 
      'appId' => '446458238729594', 
      'appSecret' => '>>>APP_SECRET<<<', 
      'user' => 0, 
      'signedRequest' => NULL, 
      'state' => NULL, 
      'accessToken' => '446458238729594|>>>APP_SECRET<<<', 
      'fileUploadSupport' => false, 
      'trustForwarded' => false, 
     )), 
      1 => '_graph', 
     ), 
     1 => 
     array (
      0 => '/me', 
     ), 
    ), 
    ), 
    4 => 
    array (
     'file' => '/srv/www/htdocs/mysite/web/index.php', 
     'line' => 69, 
     'function' => 'api', 
     'class' => 'BaseFacebook', 
     'type' => '->', 
     'args' => 
     array (
     0 => '/me', 
    ), 
    ), 
), 
    'previous' => NULL, 
)) 
-------- 

User ID is now NULL. 


=====>>>>> Session array: 
array (
    'counter' => 1, 
) 

Seems like user is NOT logged in. Getting login url...done. 

URL: 
Login/Logout 


=====>>>>> Session array: 
array (
    'counter' => 1, 
    'fb_446458238729594_state' => '6ae5ea9e5f7199fb6d19793905dcdd65', 
) 

Seems like user is still NOT logged in. At now we can't do anything. Try to login using the URL above. 

======== 

Session array: 
array (
    'counter' => 1, 
    'fb_446458238729594_state' => '6ae5ea9e5f7199fb6d19793905dcdd65', 
) 

कृपया, मुझे पता लगाने में सहायता करें कि लॉगिन क्यों काम नहीं करता है? क्यों फेसबुक क्लास का उदाहरण एक्सेस टोकन पुनर्प्राप्त नहीं करता है? और क्यों फेसबुक :: getUser() हमेशा विधि 0 देता है? मेरे द्वारा यह कैसे किया जा सकता है?

उत्तर

4

एक चाकू यहाँ लेने के लिए जा रहे हैं - तो धैर्य करें:

संभावित कारण 1: आपका अमेरिकन प्लान कोड अच्छा लग रहा है - लेकिन अपने त्रुटि कहता है: 2880:: 2050

"करने के लिए 2a03 कनेक्ट करने में विफल: 1 एफ 01: चेहरा: बी 00 सी: 0: 2: नेटवर्क पहुंच योग्य नहीं है "जो एक कर्ल मुद्दे को इंगित करता है।

क्या आप पुष्टि कर सकते हैं कि आपके कर्लर आपके वेबसर्वर पर स्थापित/काम कर रहा है? एक साधारण phpinfo(); मै तुम्हे बताऊंगा।

संभावित कारण 2: जांचें कि आपका ऐप यूआरएल उस स्थान पर सेट है जहां से आप परीक्षण चला रहे हैं। आपका रीडायरेक्ट यूआरआई "http: //mysite.localhost/" है - इसलिए सुनिश्चित करें कि आपके ऐप सेटअप पेज में यह मेल खाता है।

अन्त में,

बस मूलभूत बातों से प्रारंभ, और उसके बाद में अपने सभी अतिरिक्त कोड जोड़ना शुरू यह मेरे लिए काम करता है:।

<?php 

require 'src/facebook.php'; 

$facebook = new Facebook(array(
    'appId' => 'xxxxxxxxxxxxxxxxxxx', 
    'secret' => 'xxxxxxxxxxxxxxxxxxx', 
)); 

// See if there is a user from a cookie 
$user = $facebook->getUser(); 

if ($user) { 
    try { 
    // Proceed knowing you have a logged in user who's authenticated. 
    $user_profile = $facebook->api('/me'); 
$logoutUrl = $facebook->getLogoutUrl(); 
    } catch (FacebookApiException $e) { 
    echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>'; 
    $user = null; 
$loginUrl = $facebook->getLoginUrl($params); 
    } 
} 

?> 

<?php if ($user) { ?> 
    Your user profile is 
    <?php print htmlspecialchars(print_r($user_profile, true)) ?> 
    <a href="<?php echo $logoutUrl; ?>">Logout</a> 
<?php } else { ?> 
    <a href="<?php echo $loginUrl; ?>">Login with Facebook</a> 
<?php } ?>' 
+3

तो cURL उपलब्ध नहीं था, पीएचपी एसडीके पर मर जाएगा प्रारंभिकरण पहले से ही। मेरे लिए यह अपने सर्वर पर एक सामान्य आईपीवी 6 कनेक्टिविटी मुद्दा की तरह दिखता है; या फ़ायरवॉल या कुछ जो एफबी के सर्वर से कनेक्ट नहीं होने दे रहा है। – CBroe

+0

धन्यवाद। मुद्दा फ़ायरवॉल में था - यह मेरे स्थानीय नेटवर्क से बाहरी आईपीवी 6 कनेक्शन अवरुद्ध कर दिया। – user1764823

+0

कमाल @CBroe - अच्छा पकड़ो। – Fraccus

संबंधित मुद्दे