php – Facebook publish_stream代码以循环结束
发布时间:2020-12-13 21:46:58 所属栏目:PHP教程 来源:网络整理
导读:我有以下代码,只要在应用程序的前一部分中,用户接受对publish_stream的应用程序请求,它就可以正常工作. # The facebook libraryrequire_once("/var/www/facebook-php-sdk-master/src/facebook.php");# Create facebook object$config = array();$config['app
我有以下代码,只要在应用程序的前一部分中,用户接受对publish_stream的应用程序请求,它就可以正常工作.
# The facebook library require_once("/var/www/facebook-php-sdk-master/src/facebook.php"); # Create facebook object $config = array(); $config['appId'] = 'appId_here'; $config['secret'] = 'secret_here'; $config['fileUpload'] = false; // optional $facebook = new Facebook($config); $user_id = $facebook->getUser(); if ($user_id) { try { $user_profile = $facebook->api('/me','GET'); #check permissions $api_call = array( 'method' => 'users.hasAppPermission','uid' => $user_id,'ext_perm' => 'publish_stream' ); #set to true if true... $can_offline = $facebook -> api( $api_call ); #is it true? if( $can_offline ) { $post = array( 'message' => 'post_a_message' ); $facebook->api('/' . $_GET["id"] . '/feed','POST',$post); } else { // can't post message - don't have permission } } catch (FacebookApiException $e) { error_log($e); exit; } } else { error_log("user not logged in"); exit; } 为了尝试解决这个问题,我尝试将以下代码插入到else语句中,当前在上面的代码中只包含注释//无法发布消息 – 没有权限 我试图插入其他代码是这样的: $loginUrl = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) ); header("Location: ".$loginUrl); 只要用户接受允许我的应用程序使用publish_stream,这就有效.但是,如果用户不接受,我的应用将继续要求用户接受publish_stream.如果用户决定不接受,我该如何阻止该循环发生? 解决方法
这是工作代码:请检查:
页面名称:events.php 您可以看到$redirect_uri = https://localhost/facebook_page/events.php它将返回到同一页面. <?php $facebook_appid = "your appid"; // Facebook appplication id $facebook_secret = "your app secret"; // Facebook secret id $redirect_uri = "https://localhost/facebook_page/events.php"; // return url to our application after facebook login ## should be SAME as in facebook application $scope = "publish_stream"; // User permission for facebook $profile_id = "profile_id";// Where do you want to post it(profile id - It is a number) $code = $_REQUEST["code"]?$_REQUEST["code"]:""; if(empty($code)) { $_SESSION['state'] = rand(); // CSRF protection $dialog_url = "https://www.facebook.com/dialog/oauth?client_id=". $facebook_appid . "&redirect_uri=" . urlencode($redirect_uri) . "&state=". $_SESSION['state'] . "&scope=".$scope; header("location:".$dialog_url); } if($_SESSION['state'] && ($_SESSION['state'] == $_REQUEST['state'])) { $token_url = "https://graph.facebook.com/oauth/access_token?". "client_id=" . $facebook_appid . "&redirect_uri=" . urlencode($redirect_uri). "&client_secret=" . $facebook_secret . "&code=" . $code; $response = @file_get_contents($token_url); $params = null; parse_str($response,$params); $access_token = $params['access_token']; } ?> <!-- Here you can use message,picture,link,name,caption,description,source,place,tags as input fields--> <form enctype="multipart/form-data" method="POST" action="https://graph.facebook.com/<?php echo $profile_id;?>/feed?access_token=<?php echo $access_token; ?>"> <input type="text" name="message" value="test" /> <input type="submit" name="submit" value="Submit" /> </form> 你也可以使用jquery发布它. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |