头图

前言

最近晓杰在做一个管理平台,其中有上传图片素材到公众号返回media_id 的需求,将之前写过的代码直接复制粘贴,进行一顿操作后测试发现返回为空!

过程

代码如下

 public static function upload($url, $filedata) {

        $curl = curl_init ();
        if (class_exists('\CURLFile')) {
            $data['media'] = new \CURLFile($filedata);
        } else {
            $data['media'] = '@'.realpath($filedata);
        }
        curl_setopt ( $curl, CURLOPT_URL, $url );
        curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, false );
        if (! empty ( $data )) {
            curl_setopt ( $curl, CURLOPT_POST, 1 );
            curl_setopt ( $curl, CURLOPT_POSTFIELDS, $data );
        }
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
        $output = curl_exec ( $curl );
        curl_close ( $curl );
        return $output;

    }

原因

晓杰就一直百度查找正确原因,直到查到很多人用PHP7.4遇到同样的问题,我就醒悟了,更换了PHP版本尝试了一下 果然可以了!但是这并不能解决实际问题!

解决它!

经过一般努力尝试感谢一位小伙伴分享了他的解决方案,晓杰粘贴出来供大家参考!

 function api_upload(string $url, array $params = []): string
    {
        $timeout = 20;
        $urls = parse_url($url);
        $scheme = $urls['scheme'];
        $port = $urls['port'] ?? ($scheme == 'https' ? 443 : 80);
        $host = $urls['host'];


        $boundary = '---------------------------' . substr(md5(rand(0, 32000)), 0, 10);
        $data = '--' . $boundary . "\r\n";


        foreach ($params as $key => $val) {
            if ($val instanceof CURLFile) {
                $filename = $val->getPostFilename();
                $mimetype = $val->getMimeType();
                $content = file_get_contents($val->getFilename());


                $data .= "content-disposition: form-data; name=\"" . $key . "\"; filename=\"" . $filename . "\"\r\n";
                $data .= "content-type: " . $mimetype . "\r\n\r\n";
                $data .= $content . "\r\n";
                $data .= "--" . $boundary . "\r\n";
            } else {
                $data .= "Content-Disposition: form-data; name=\"" . $key . "\"\r\n";
                $data .= "Content-type:text/plain\r\n\r\n";
                $data .= rawurlencode($val) . "\r\n";
                $data .= "--$boundary\r\n";
            }
        }


        $data .= "--\r\n\r\n";


        $out = "POST " . $url . " HTTP/1.1\r\n";
        $out .= "host: " . $host . "\r\n";
        $out .= "content-type: multipart/form-data; boundary=" . $boundary . "\r\n";
        $out .= "content-length: " . strlen($data) . "\r\n";
        $out .= "connection: close\r\n\r\n";
        $out .= $data;


        if ($scheme == 'https') {
            $context = stream_context_create([
                'ssl' => [
                    'verify_peer' => false,
                    'verify_peer_name' => false
                ]
            ]);


            $hostname = "ssl://$host:$port";
            $fp = stream_socket_client($hostname, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $context);
        } else {
            $fp = fsockopen($host, $port, $errno, $errstr, $timeout);
        }


        fputs($fp, $out);
        $res = '';
        while ($row = fread($fp, 4096)) {
            $res .= $row;
        }
        fclose($fp);


        $pos = strpos($res, "\r\n\r\n");
        $res = substr($res, $pos + 4);


        return $res;
    }

调用代码

$url = 'https://api.weixin.qq.com/cgi-bin/media/upload?access_token=' .self::getAccessToken().'&type=' . $type;
        $result = self::api_upload($url,[
            'media' => new CURLFile($localFilePath, $Mine, $localFilePath),
        ]);

至此问题解决,希望能帮到大家


Soujer
15 声望10 粉丝

热爱分享,热爱创作,热爱研究。