脚本宝典收集整理的这篇文章主要介绍了php – Facebook 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.如果用户决定不接受,我该如何阻止该循环发生?
您可以看到$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发布它.
以上是脚本宝典为你收集整理的php – Facebook publish_stream代码以循环结束全部内容,希望文章能够帮你解决php – Facebook publish_stream代码以循环结束所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。