使用WordPress wp_remote_post调用Zoho WorkDrive API参数过多报错如何精简参数?
解决WordPress wp_remote_post调用Zoho WorkDrive API上传文件的参数过多错误
我参照Zoho WorkDrive API实现文件上传功能,用原生PHP CURL代码可以成功上传,但改用WordPress的wp_remote_post方法时,始终返回错误:
{"errors":[{"id":"F6012","title":"Number of parameters is more than specified"}]}
代码重现
原生CURL成功代码
$folderId = 'xxxx'; $token = 'xxxx'; $filepath = 'C:\\Users\\Me\\Pictures\\test-file.jpg'; $realPath = realpath( $filepath ); $content = curl_file_create( $realPath ); //the parameters $postFields = array( 'filename' => 'test.jpg', 'parent_id' => $folderId, 'override-name-exist' => 'true', 'content' => $content ); $curl_url = 'https://workdrive.zoho.com/api/v1/upload'; $curl_var = curl_init(); curl_setopt( $curl_var, CURLOPT_URL, $curl_url ); curl_setopt( $curl_var, CURLOPT_POST, 1 ); curl_setopt( $curl_var, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt( $curl_var, CURLOPT_SSL_VERIFYPEER, 0 ); curl_setopt( $curl_var, CURLOPT_POSTFIELDS, $postFields ); curl_setopt( $curl_var, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer ' . $token, 'cache-control: no-cache' ) ); $response = curl_exec( $curl_var ); curl_close( $curl_var ); echo $response;
失败的wp_remote_post代码
$folderId = 'xxxx'; $token = 'xxxx'; $filepath = 'C:\\Users\\Me\\Pictures\\test-file.jpg'; $url = 'https://workdrive.zoho.com/api/v1/upload'; $realPath = realpath( $filepath ); $content = curl_file_create( $realPath ); //the parameters $postFields = array( 'filename' => 'test.jpg', 'parent_id' => $folderId, 'override-name-exist' => 'true', 'content' => $content ); $send = array( 'headers' => array( 'cache-control' => 'no-cache', 'authorization' => 'Bearer ' . $token, ), 'body' => $postFields ); $response = wp_remote_post( $url, $send ); if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != 200 ) { error_log( print_r( $response, true ) ); } return json_decode( wp_remote_retrieve_body( $response ) );
我曾尝试添加user-agent、sslverify等参数设为false,但没有效果。
解决方案
问题核心在于请求编码格式的差异:
- 原生CURL处理包含
CURLFile的数组时,会自动以multipart/form-data格式发送请求,符合Zoho API的要求; wp_remote_post默认会将body数组编码为application/x-www-form-urlencoded格式,导致文件参数被错误解析,触发API的"参数过多"错误。
只需要在请求参数中添加'multipart' => true,强制以multipart/form-data格式发送请求,同时保留sslverify => false(对应原生CURL的CURLOPT_SSL_VERIFYPEER=0)即可解决问题。修改后的$send数组如下:
$send = array( 'headers' => array( 'cache-control' => 'no-cache', 'authorization' => 'Bearer ' . $token, ), 'body' => $postFields, 'multipart' => true, // 关键:强制使用multipart/form-data格式 'sslverify' => false );
修改后,wp_remote_post的请求行为与原生CURL一致,API能正确识别所有参数,不会再返回参数过多的错误。
内容的提问来源于stack exchange,提问作者contemplator
相关产品推荐
相关产品推荐

