php版本区别:

    1. <=5.4 curl上传文件只支持@语法
    2. = 5.5 支持@语法和CURLFile类
    3. 大于=5.6 只支持CURLFile类

    // 兼容性写法参考示例

    1. $curl = curl_init();
    2. if (class_exists('\CURLFile')) {// 这里用特性检测判断php版本
    3. curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
    4. $data = array('file' => new \CURLFile(realpath($source)));//>=5.5
    5. } else {
    6. if (defined('CURLOPT_SAFE_UPLOAD')) {
    7. curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
    8. }
    9. $data = array('file' => '@' . realpath($source));//<=5.5
    10. }
    11. curl_setopt($curl, CURLOPT_URL, $url);
    12. curl_setopt($curl, CURLOPT_POST, 1 );
    13. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    14. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    15. curl_setopt($curl, CURLOPT_USERAGENT,"TEST");
    16. $result = curl_exec($curl);
    17. $error = curl_error($curl);