php用curl上传图片微信永久素材源码

用php写的一个用curl上传图片到微信永久素材的源码,可修改成临时素材,只要修改链接就可以了

<?php
ini_set("display_errors", "On");
 function add_material($filename) {
        $file_info = array('filename' => $filename, //图片相对于网站根目录的路径
        // $file_info = 'image/1.jpg', //图片相对于网站根目录的路径
        'content-type' => 'image/jpg', //文件类型
        //'filelength' => '71' //图文大小
        );
        //var_dump($file_info);
        $access_token ="qoE0KuAhdC2uQSYylziw7Q0d3uDNjFoNHZVE7CjX_eLnEufWF7k1KAZ7K76HvBVFOcYDoHf9EP4GKeGb5ctN7QR94ghT3frox66E-r6xZT-wMzdg9pUnT_yWVDrfgeOyCKUiAHARZJ";
        $url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={$access_token}&type=image";
        $ch1 = curl_init();
        $timeout = 5;
        $real_path = "{$_SERVER['DOCUMENT_ROOT']}{$file_info['filename']}";
        //$real_path=str_replace("/", "//", $real_path);
        $data = array("media" => "@{$real_path}", 'form-data' => $file_info);
        curl_setopt($ch1, CURLOPT_URL, $url);
        curl_setopt($ch1, CURLOPT_POST, 1);
        curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch1, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch1, CURLOPT_POSTFIELDS, $data);
        $result = curl_exec($ch1);
        curl_close($ch1);

            $result = json_decode($result, true);
            var_dump($result);
            return $result['media_id'];
    }
    $filename=$_GET['url'];
    //echo $filename;
    echo add_material($filename);

php用curl上传图片微信永久素材源码:等您坐沙发呢!

发表评论

表情
还能输入210个字