如何使用这个PHP从网站发布到Facebook页面

时间:2014-08-09 16:03:43

标签: php facebook facebook-graph-api curl

所以我有facebook.php和generatePageToken.php。使用下面的代码,我在控制台中获得了一个访问令牌。在我发布facebook和generatePageToken后,我将给出一个PHP脚本,这个脚本是支持我发布到我的Facebook页面作为我的页面。不是管理员

facebook.php

<!doctype html>
<html>
<head>
        <script type="text/javascript">

        function SendToFacebook()
        {
            window.fbAsyncInit = function () {
                // init the FB JS SDK
                FB.init({
                    appId: '432036336937975',                        // App ID from the app dashboard
                    status: false,                                 // Check Facebook Login status
                    xfbml: true                                  // Look for social plugins on the page
                });


                FB.login(function (response) {
                    FB.api('/me/accounts', function (apiresponse) {
if (response.authResponse) {
        //simple user access token
        var accessToken = response.authResponse.accessToken,
            ajaxRequest = new XMLHttpRequest(),
            pageId = '1521533601413118';

        ajaxRequest.onreadystatechange = function() {
            if(ajaxRequest.readyState === 4) {
                //print out the extended page access token
                alert(ajaxRequest.responseText);
            }
        };
        ajaxRequest.open('POST','generatePageToken.php?pageId=' + pageId, true);
        ajaxRequest.setRequestHeader('Content-type','application/x-www-form-urlencoded');
        ajaxRequest.send('accessToken=' + accessToken);
    }

                        var data = {
                            message: "mymessage test",
                            display: 'iframe',
                            caption: "caption",
                            name: "name",
                            description: "description",
                            to: '1521533601413118',
                            from: '1521533601413118'
                        };

                        FB.api('/1521533601413118/feed', 'post', data, function () {
                            console.log(arguments);
                        });


                    });

                }, { scope: 'manage_pages, publish_stream'});

            };
            // Load the SDK asynchronously
            (function (d, s, id) {
                var js, fjs = d.getElementsByTagName(s)[0];
                if (d.getElementById(id)) { return; }
                js = d.createElement(s); js.id = id;
                js.src = "//connect.facebook.net/en_US/all.js";
                fjs.parentNode.insertBefore(js, fjs);
            } (document, 'script', 'facebook-jssdk'));
        }
        </script>

</head>
<body>
<a href="#" onclick="SendToFacebook();">Submit</a>
<div id="fb-root"></div>

</body>
</html>

generatePageToken.php

<?php
$accessToken = $_POST['accessToken'];
$pageId = $_GET['pageId'];
$fbAppId = '432036336937975';
$fbAppSecret = 'REMOVED';

$appsecretProof = hash_hmac('sha256', $accessToken, $fbAppSecret);
//init curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'facebook-php-3.2');

//get extended user access token
$url = 'https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token' .
    '&client_id=' . $fbAppId .
    '&client_secret=' . $fbAppSecret .
    '&fb_exchange_token=' . $accessToken .
    '&appsecret_proof=' . $appsecretProof;
curl_setopt($ch, CURLOPT_URL, $url);
$curlResult = curl_exec($ch);
$response_params = array();
parse_str($curlResult, $response_params);
$extendedUserToken = $response_params['access_token'];

$appsecretProof = hash_hmac('sha256', $extendedUserToken, $fbAppSecret);
//get extended page access token
$url = 'https://graph.facebook.com/' . $pageId .
    '?fields=access_token' .
    '&access_token=' . $extendedUserToken .
    '&appsecret_proof=' . $appsecretProof;
curl_setopt($ch, CURLOPT_URL, $url);
$curlResult = curl_exec($ch);
curl_close($ch);
$pageToken = json_decode($curlResult)->access_token;



echo $pageToken;
?>

现在用于PHP脚本

$data = http_build_query(array(
    'access_token'  => **your access token**,
    'message'       => 'hello world!'
));
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/v2.1/1521533601413118/feed');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
curl_close($ch);

它表示&#34;您的访问令牌&#34;如何将我通过上述页面获取的访问令牌放入INTO?另外我在哪里发布这个PHP脚本,使其发布到我的页面。有人可以整合这个吗?请帮忙!!!!!!!!!!!!

0 个答案:

没有答案