在 /media Whatsapp 云 api 上上传媒体文件时出现问题

问题描述 投票:0回答:1

我正在尝试通过 WhatsApp Cloud API 上传文件,因此我使用简单的 cURL 请求来执行此操作。

public function meta_upload_media2($token,$phonesid,$tipo,$archivo,$filename)
    {        

        $ch = curl_init();
        $url = 'https://graph.facebook.com/v18.0/'.$phonesid.'/media';

        $fileFinal=new \CURLFile($archivo,$tipo,$filename);
        
        $data = array(
            'file' => $fileFinal,
            'type' => $tipo,
            'messaging_product' => 'whatsapp'
        );
        $headers = array(); 
        
        $headers[] = 'Authorization: Bearer ' . $token ;

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        
        $result = curl_exec($ch);        

        if($result === false) {
            $result = curl_error($ch) . " - ".curl_errno($ch);
            //return false;
            
        }else{
            $resultDecode = json_decode($result);
            if($resultDecode!=null){
                $result = $resultDecode;
            }
            return($result);
            //return true;
        }
        curl_close($ch);
    }  

我无法上传文件,出现以下错误:

“错误”:{ "message": "发生未知错误。", “类型”:“OAuthException”, “代码”:1, "fbtrace_id": "ActSWlPfhEOVpbiN2QmIf7X" }

仅供参考: ** 我使用的是 CI4 和 PHP 8.1 ** $filename 是随机文件名 ** $archivo 是完整路径文件,例如。 /home/user/public_html/uploads/random-file-name.pdf

非常感谢任何帮助。 谢谢!

php curl php-curl codeigniter-4 whatsapp-cloud-api
1个回答
0
投票

使用以下代码解决了

$ch = curl_init();
        $url = 'https://graph.facebook.com/v18.0/'.$phonesid.'/media';
        
        $data = array(
            'file' => new \CURLFile($archivo,$type),
            'type' => $type,
            'messaging_product'=>'whatsapp'
        );
        $headers = array(); 
        
        $headers[] = 'Authorization: Bearer ' . $token ;
        $headers[] = 'Content-type: multipart/form-data';

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        
        $result = curl_exec($ch);        

        if($result === false) {
            $result = curl_error($ch) . " - ".curl_errno($ch);
            //return false;
            
        }else{
            $resultDecode = json_decode($result);
            if($resultDecode!=null){
                $result = $resultDecode;
            }
            return($result);
            //return true;
        }
        curl_close($ch);
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.