-1

I would like to call the shareFB() method to auto share something on a Facebook Page which I manage. And maybe pass it data shareFB($link, $message)

Currently getting Parse error: syntax error, unexpected 'use' (T_USE).

How can I set this up to work?

public function shareFB() {
 require_once( ''.dirname(__DIR__).'/includes/facebook-php-sdk-v4-4.0-dev/src/Facebook/FacebookRequest.php' );
use Facebook\FacebookRequest;

try {

  $response = (new FacebookRequest(
  $session, 'POST', '/me/feed', array(
            'link' => 'https://www.youtube.com/watch?v=C2FETG7tCF0',
            'message' => 'The Sun Full HD 1080p, Amazing Documentary'
                )
            ))->execute()->getGraphObject();

            echo "Posted with id: " . $response->getProperty('id');

        } catch(FacebookRequestException $e) {

            echo "Exception occured, code: " . $e->getCode();
            echo " with message: " . $e->getMessage();

        }
}

EDIT ***

    require_once 'Db.class.php';
    require_once( ''.dirname(__DIR__).'/includes/facebook-php-sdk-v4-4.0-dev/src/Facebook/FacebookRequest.php' );
    use Facebook\FacebookRequest;

  $app_id = 'xxx'; //Facebook App ID
  $app_secret = 'xxx'; //Facebook App Secret
  $redirect_url = 'http://example.com/com/login'; //FB redirects to this page with a code

  //initialize Facebook
  FacebookSession::setDefaultApplication($app_id, $app_secret);
  $helper = new FAcebookRedirectLoginHelper($redirect_url);

  try {
    $session = $helper->getSessionFromRedirect();
  } catch(FacebookRequestException $ex) {
    // When Facebook returns an error
    die(" Error : " . $ex->getMessage());
  } catch(\Exception $ex) {
    // When validation fails or other local issues
    die(" Error : " . $ex->getMessage());
  }

    class HelperClass {

    public function shareFB() {
                try {

                    $response = (new FacebookRequest(
                        $session, 'POST', '/me/feed', array(
                            'link' => 'https://www.youtube.com/watch?v=C2FETG7tCF0',
                            'message' => 'The Sun Full HD 1080p, Amazing Documentary'
                        )
                    ))->execute()->getGraphObject();

                    echo "Posted with id: " . $response->getProperty('id');

                } catch(FacebookRequestException $e) {

                    echo "Exception occured, code: " . $e->getCode();
                    echo " with message: " . $e->getMessage();

                }
        }

    }
Ciprian
  • 3,066
  • 9
  • 62
  • 98

2 Answers2

1

http://php.net/manual/en/language.namespaces.importing.php

The use keyword must be declared in the outermost scope of a file (the global scope) or inside namespace declarations. This is because the importing is done at compile time and not runtime, so it cannot be block scoped.

Move the use declaration outside of your shareFB function.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
0

Try this:

require_once(dirname(__DIR__) . '/includes/facebook-php-sdk-v4-4.0-dev/src/Facebook/FacebookRequest.php');
use Facebook\FacebookRequest;

public function shareFB() {
...

For posting to a page, you need publish_pages and manage_pages and you need to use a Page Token to post "as Page". But that´s not the problem in this case - it only may be a problem after you fixed the problem at hand, so i added it just in case ;)

About using the PHP SDK in general:

A lot of stuff is missing from your code. You need to authorize with the correct permissions (see above) and you need to generate a Page Token. Make sure you know about all that stuff.

Here are some additional links about Tokens:

andyrandy
  • 72,880
  • 8
  • 113
  • 130
  • Thank you. I just did what you suggested and I got past that error. But now i'm getting an undefined $session. I use Facebook login and I already have it set up. Do I create another FB session in the helper class? Or should I try to use my existing one? – Ciprian May 27 '15 at 16:50
  • well, you did not create any session yet. i will include some links to tutorials and example code. – andyrandy May 27 '15 at 16:59
  • please see my edit. I do all that already in a global.inc.php file. Which I use in regular pages. But I just don't know how to use or include that code in this class. I wouldn't want to write something twice. – Ciprian May 27 '15 at 17:02
  • well, in that case you need to debug your session. – andyrandy May 27 '15 at 17:03
  • btw, try without that helper class, just put the code outside – andyrandy May 27 '15 at 17:05
  • I think I can get it to work outside. But I want it to run when someone posts an item on the website. – Ciprian May 27 '15 at 17:06
  • 1
    no ... ignore my last comment. I think I know how to do it. I ll just put the code outside and not use the helper class :)) – Ciprian May 27 '15 at 17:07