FB API PHP curl_setopt_array():不推荐使用@filename API进行文件上载

时间:2013-08-28 07:54:59

标签: facebook-graph-api facebook-php-sdk

将FB照片上传到服务器时,FB PHP API和php 5.5出现问题。使用方法时:

 private function _upload( $type = '', $path = '', $message = '', $aid = '' ) {
        try {

            if ( !in_array( $type, array( 'photos', 'videos' ) ) ) {
                throw new \Exception( 'Error: Incorrect type ' );
            }

            if ( !self::getFB()->getUser() ) {
                throw new \Exception( 'Error: No user' );
            }

            if ( empty( $path ) ) {
                throw new \Exception( 'Error: path is empty' );
            }
            if ( !file_exists( realpath( $path ) ) ) {
                throw new \Exception( 'Error: file doesn\'t exists' );
            }


            if ( !empty( $aid ) ) {
                $url = "/" . $aid . "/" . $type;
            } else {
                $url = '/' . self::getFB()->getUser() . '/' . $type;
            }

            var_dump( array( $url, 'POST',

                    array(
                        'image'   => '@' . realpath( $path ),
                        'message' => $message,
                    )
                )
            );

            self::getFB()->setFileUploadSupport( TRUE );

            $ret_obj = self::getFB()->api( $url, 'POST', array(
                    'image'   => '@' . realpath( $path ),
                    'message' => $message,
                )
            );
            return $ret_obj[ 'id' ];
        } catch ( \Exception $e ) {
            return $e->getMessage();
        }
    }

我收到错误:curl_setopt_array():不推荐使用@filename API进行文件上传。请改用CURLFile类

1 个答案:

答案 0 :(得分:3)

您正在使用PHP 5.5以及通过CURL上传的旧方法。查看https://wiki.php.net/rfc/curl-file-upload

上的差异

curl_setopt($curl_handle, CURLOPT_POST, 1);
$args['file'] = '@filename.png';
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $args);

curl_setopt($curl_handle, CURLOPT_POST, 1);
$args['file'] = new CurlFile('filename.png', 'image/png');
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $args);