在命令行中执行PHP脚本时,它返回脚本标记中的webscript

时间:2016-10-25 14:17:58

标签: php facebook-graph-api

正如标题所示,我正在尝试在linux命令行上执行一个PHP文件并且它工作正常,我实际上运行PHP脚本在Facebook图形API上发布状态,并且在执行脚本时我得到了一个在脚本标签中包含的webscript,我得到的响应,如何执行响应?如何使用浏览器自动执行它?

我的最终目标是使用cron选项卡通过调用此PHP脚本来安排发布facebook状态。

用于发布的PHP代码

> $fb = new Facebook\Facebook([
  'app_id' => '1784504901790451',
  'app_secret' => '4b7b2673ecd268b962b0f25166955f23',
  'default_graph_version' => 'v2.8',
]);

> $helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'publish_actions']; // optional
try {
        if (isset($_SESSION['facebook_access_token'])) {
                $accessToken = $_SESSION['facebook_access_token'];
        } else {
                $accessToken = $helper->getAccessToken();
        }
} catch(Facebook\Exceptions\FacebookResponseException $e) {
        // When Graph returns an error
        echo 'Graph returned an error: ' . $e->getMessage();
        exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
        // When validation fails or other local issues
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        exit;
 }
if (isset($accessToken)) {
        if (isset($_SESSION['facebook_access_token'])) {
                $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
        } else {
                $_SESSION['facebook_access_token'] = (string) $accessToken;
                // OAuth 2.0 client handler
                $oAuth2Client = $fb->getOAuth2Client();
                // Exchanges a short-lived access token for a long-lived one
                $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
 $_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
                $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
        }

        // validating the access token
        try {
                $request = $fb->get('/me');
        } catch(Facebook\Exceptions\FacebookResponseException $e) {
                // When Graph returns an error
                if ($e->getCode() == 190) {
                        unset($_SESSION['facebook_access_token']);
                        $helper = $fb->getRedirectLoginHelper();
                        $loginUrl = $helper->getLoginUrl('http://localhost/AutoPHPfb/', $permissions);
                        echo "<script>window.top.location.href='".$loginUrl."'</script>";
                        exit;
                }
        } catch(Facebook\Exceptions\FacebookSDKException $e) {
                // When validation fails or other local issues
                echo 'Facebook SDK returned an error: ' . $e->getMessage();
 exit;
        }

        // posting on user timeline using publish_actins permission
        try {
                // message must come from the user-end
            $data = [
  'message' => 'My awesome photo upload example.',
  'source' => $fb->fileToUpload('/var/www/bitvie.cloudapp.net/htdocs/AutoPHPfb/Photos/B612-2015-08-10-10-36-17.jpg'),
  // Or you can provide a remote file location
  //'source' => $fb->fileToUpload('https://example.com/photo.jpg'),
];
                $request = $fb->post('/me/photos', $data);
                $response = $request->getGraphNode();
        } catch(Facebook\Exceptions\FacebookResponseException $e) {
                // When Graph returns an error
                echo 'Graph returned an error: ' . $e->getMessage();
                exit;
        } catch(Facebook\Exceptions\FacebookSDKException $e) {
            // When validation fails or other local issues
                echo 'Facebook SDK returned an error: ' . $e->getMessage();
                exit;
        }

        echo $response['id'];

        // Now you can redirect to another page and use the
        // access token from $_SESSION['facebook_access_token']
} else {
        $helper = $fb->getRedirectLoginHelper();
        $loginUrl = $helper->getLoginUrl('http://bitvie.cloudapp.net/AutoPHPfb/PHPprofilepic.php', $permissions);
        echo "<script>window.top.location.href='".$loginUrl."'</script>";
}

这是我得到的回应

<script>window.top.location.href='https://www.facebook.com/v2.8/dialog/oauth?client_id=1784504901790451&state=4a090fd3336bc798d3604bc760538e3f&response_type=code&sdk=php-sdk-5.0.0&redirect_uri=http%3A%2F%2Fbitvie.cloudapp.net%2FAutoPHPfb%2FPHPprofilepic.php&scope=email%2Cpublish_actions'</script>

0 个答案:

没有答案