首頁 > 後端開發 > PHP7 > 主體

php7中的curl檔案上傳出現錯誤該怎麼辦

醉折花枝作酒筹
發布: 2023-02-18 07:56:01
轉載
2487 人瀏覽過

這篇文章要為大家介紹解php7中curl檔案上傳出現錯誤的方法。有一定的參考價值,有需要的朋友可以參考一下,希望對大家有幫助。

php7中的curl檔案上傳出現錯誤該怎麼辦

最近在專案跟微信公眾號的素材庫對接介面,採用curl的post方式提交素材檔案,發現一直提示

{“errcode”:41005,”errmsg”:”media data missing”}

程式碼內容

$url = self::$add_material . $accessToken . '&type=' . $key;
$data = [
            'media' => '@' . $fileName,
            'form-data' => $fileInfo,
            'description' => json_encode([
                'title' => $fileName,
                'introduction' => ''
            ]),
        ];
self::init($url);
$data = is_array($data) ? http_build_query($data) : $data;
curl_setopt(self::$curl, CURLOPT_POST, 1);
curl_setopt(self::$curl, CURLOPT_POSTFIELDS, $data);
$info = curl_exec(self::$curl);
curl_close(self::$curl);
登入後複製

查閱了官方文件在php5.5後不再支援@,必須使用CurlFile或設定CURLOPT_SAFE_UPLOAD為1

There are “@” issue on multipart POST requests.
Solution for PHP 5.5 or later:
Enable CURLOPT_SAFE_UPLOAD. ## inse CULOAD. “@”.

在php7 curl如果改變CURLOPT_SAFE_UPLOAD會提示一個錯誤如下: 

curl_setopt(): Disabling safe uploads is no longer supported in 報錯

##我們只能老實使用CurlFile來處理

$url = self::$add_material . $accessToken . '&type=' . $key;
$data = [
            'media' => new \CURLFile($fileName),
            'form-data' => $fileInfo,
            'description' => json_encode([
                'title' => $fileName,
                'introduction' => ''
            ]),
        ];
self::init($url);
$data = is_array($data) ? http_build_query($data) : $data;
curl_setopt(self::$curl, CURLOPT_POST, 1);
curl_setopt(self::$curl, CURLOPT_POSTFIELDS, $data);
$info = curl_exec(self::$curl);
curl_close(self::$curl);
登入後複製

然後發現這樣寫三個大坑(是我自己蠢)

1、如果CURLOPT_POSTFILEDS傳入的是陣列content_type就為multipart/form-data;如果CURLOPT_POSTFILEDS傳入的是json或key-value& content_type就為x-www-form_urlencoded;但是微信支援form-data傳遞的陣列 

2、陣列裡面如果有包含物件對其進行http_build_query會將其改成陣列 

3、CurlFile只能讀取伺服器內的路徑,如果要上傳網路上的位址,需要先下載到伺服器的暫存目錄,在通過CurlFile讀取檔案路徑(絕對路徑)

所以我們接著調整程式碼

$url = self::$add_material . $accessToken . '&type=' . $key;
$data = [
            'media' => new \CURLFile($fileName),
            'form-data' => $fileInfo,
            'description' => json_encode([
                'title' => $fileName,
                'introduction' => ''
            ]),
        ];
self::init($url);
curl_setopt(self::$curl, CURLOPT_POST, 1);
curl_setopt(self::$curl, CURLOPT_POSTFIELDS, $data);
$info = curl_exec(self::$curl);
curl_close(self::$curl);
登入後複製

正當我以為我可以解脫的時候,php7這裡彈出一個notice語法錯誤:

Array to string conversion 

然後查閱了資料發現CURLOPT_POSTFIEDLDS不支援多維數組 

但是提示的notice的語法錯誤,我們完全可以進行屏蔽 

# #繼續調整程式碼

$url = self::$add_material . $accessToken . '&type=' . $key;
$data = [
            'media' => new \CURLFile($fileName),
            'form-data' => $fileInfo,
            'description' => json_encode([
                'title' => $fileName,
                'introduction' => ''
            ]),
        ];
self::init($url);
curl_setopt(self::$curl, CURLOPT_POST, 1);
@curl_setopt(self::$curl, CURLOPT_POSTFIELDS, $data);
$info = curl_exec(self::$curl);
curl_close(self::$curl);
登入後複製
結果終於上傳素材成功了 

抬頭一望天已黑 

開心我趕緊一邊擦鼻涕一邊收拾東西下班

#推薦學習:

php影片教學#

以上是php7中的curl檔案上傳出現錯誤該怎麼辦的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:csdn.net
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!