4

I just tried to develop a Facebook application using Facebook PHP SDK. The example code given in Facebook developer site is as below..

        <?php
  // Remember to copy files from the SDK's src/ directory to a
  // directory in your application on the server, such as php-sdk/
  require_once('php-sdk/facebook.php');

  $config = array(
    'appId' => 'YOUR_APP_ID',
    'secret' => 'YOUR_APP_SECRET',
    'fileUpload' => true,
    'allowSignedRequest' => false // optional but should be set to false for non-canvas apps
  );

  $facebook = new Facebook($config);
  $user_id = $facebook->getUser();

  $photo = './mypic.png'; // Path to the photo on the local filesystem
  $message = 'Photo upload via the PHP SDK!';
?>
<html>
  <head></head>
  <body>

  <?php
    if($user_id) {

      // We have a user ID, so probably a logged in user.
      // If not, we'll get an exception, which we handle below.
      try {

        // Upload to a user's profile. The photo will be in the
        // first album in the profile. You can also upload to
        // a specific album by using /ALBUM_ID as the path 
        $ret_obj = $facebook->api('/me/photos', 'POST', array(
                                         'source' => new CURLFile($photo, 'image/png'),
                                         'message' => $message,
                                         )
                                      );
        echo '<pre>Photo ID: ' . $ret_obj['id'] . '</pre>';
        echo '<br /><a href="' . $facebook->getLogoutUrl() . '">logout</a>';
      } catch(FacebookApiException $e) {
        // If the user is logged out, you can have a 
        // user ID even though the access token is invalid.
        // In this case, we'll get an exception, so we'll
        // just ask the user to login again here.
        $login_url = $facebook->getLoginUrl( array(
                       'scope' => 'photo_upload'
                       )); 
        echo 'Please <a href="' . $login_url . '">login.</a>';
        error_log($e->getType());
        error_log($e->getMessage());
      }   
    } else {

      // No user, print a link for the user to login
      // To upload a photo to a user's wall, we need photo_upload  permission
      // We'll use the current URL as the redirect_uri, so we don't
      // need to specify it here.
      $login_url = $facebook->getLoginUrl( array( 'scope' => 'photo_upload') );
      echo 'Please <a href="' . $login_url . '">login.</a>';

    }

  ?>

  </body>
</html>

But when I run the program it shows an error 'Fatal error: Class 'CURLFile' not found'. When searched for the solution I find that the new PHP Facebook SDK doesn't have 'CURLFile' Class. Can anyone help me with the new SDK code to 'Post Photo to the user timeline'?

Thanks in advance..

Ashique Majeed
  • 39
  • 1
  • 1
  • 6

4 Answers4

11

Maybe you need to add a backslash:

new \CurlFile($photo)

If it does not work maybe checking if CurlFile exists. If it does not exist then you need pass directly the tmp filename.fileExtension.

'source' => class_exists('CurlFile', false) ? new CURLFile($photo, 'image/png') : "@{$photo}"

where your $photo in the else condition is in the form of filename.fileExtension.

jhnferraris
  • 1,361
  • 1
  • 12
  • 34
3

The class CURLFile is only available in PHP 5 >= 5.5.0

As Facebook example says:

// If you're not using PHP 5.5 or later, change the file reference to:
// 'source' => '@/path/to/file.name'
Pablo
  • 635
  • 1
  • 7
  • 19
1

Did you try enabling cURL in PHP by uncomment the line

;extension=php_curl.dll

in xampp\apache\bin\php.ini, and then restart the Apache service.

you can check that by phpinfo()

also check your php version based on The CURLFile class doc

HMagdy
  • 3,029
  • 33
  • 54
0

Try this instead:

$ret_obj = $facebook->api('/me/photos', 'POST', [
    'image'   => '@' . realpath($photo),
    'message' => '$message'
]);

This worked for me.

In addition to using realpath, note that 'source' is now 'image'.

See:
Working with Facebook PHP SDK: Uncaught CurlException: 26: failed creating formpost dat

Community
  • 1
  • 1
Paul Brady
  • 503
  • 4
  • 10