cURL上傳圖片等文件使用CURLFile class 替代 @filename API

在使用cURL上傳圖片等文件時,利用cURL方法,傳統的上傳方法。

//圖片信息
    $img_path = dirname(__FILE__).'\boy.jpg';
    $img_data = array(
        'media'=>'@'.$img_path
    );
    $res = $Wechat->uploadImg(access_token,$img_data);
    var_dump($res);

此處會報錯:Deprecated: curl_setopt_array(): The usage of the @filename API for file uploading is deprecated. Please use the CURLFile class instead……

使用CURLFile class的更正方法:

    $img_path = 'C:\Users\DELL-PC\Desktop\boy.jpg';
    $cfile = curl_file_create($img_path);   //use the CURLFile Class 替換@的使用方法。
    $img_data = array(
        'media'=>$cfile
    );
    $res = $Wechat->uploadImg(access_token,$img_data);
    var_dump($res);

這樣即可解決warning的問題!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章