'Fatal error: class' CURLFile 'not found' error on Facebook PHP application for uploading photos to the user timeline

I just tried to develop a Facebook application using the PHP PHP SDK. An example code is provided on the Facebook developer site below.

        <?php
  // Remember to copy files from the SDK 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 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 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 you were looking for a solution, I found that the new PHP Facebook SDK does not have a CURLFile class. Can someone help me with a new SDK code to “send a photo to the user's timeline”?

Thanks in advance.

+4
source share
4

, :

new \CurlFile($photo)

, , CurlFile. , tmp filename.fileExtension.

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

$photo else filename.fileExtension.

+9

CURLFile PHP 5 >= 5.5.0

Facebook :

// If you're not using PHP 5.5 or later, change the file reference to:
// 'source' => '@/path/to/file.name'
+3

cURL PHP,

;extension=php_curl.dll

xampp\apache\bin\php.ini, Apache.

phpinfo()

php CURLFile

+1

:

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

.

In addition to use realpath, please note that 'source'now 'image'.

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

0
source

Source: https://habr.com/ru/post/1531582/


All Articles