公众号--临时素材上传

<form action="/loupan/{$_hid}/testsave.html" method="post"  enctype="multipart/form-data">

<!--    <p><input type="file" name="file"></p>-->
    <p><input type="file" id="media" name="file"></p>
    <p><button type="submit">提交</button></p>
</form>
  //上传图文消息内的图片获取URL
       $url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={$access_token}";
    $file = $_FILES['file'];
    $path = $file['tmp_name'];
    $name = $file['name'];
    $type = $file['type'];
    $cfile = new CURLFile($path,$type,$name);
    $a = $wechat->http_post($url,["media"=>$cfile],true);//返回图片url,url再微信浏览器内才可打开
//临时文件--image 
 $file = $_FILES['file'];
        $path = $file['tmp_name'];
        $name = $file['name'];
        $type = $file['type'];
        $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$access_token}&type=image";
        $cfile = new CURLFile($path,$type,$name);
        $a = $wechat->http_post($url,["media"=>$cfile],true);

        var_dump($a);

返回数据{"type":"image","media_id":"Tld5m4MzOyqqiw0QmQufB85uinfAYsp72AqL2zj_J6FkqkNrfAfwdTY5GopjuIkZ","created_at":1678679960,"item":[]}

//临时文件--video
 $file = $_FILES['file'];
           $path = $file['tmp_name'];
           $name = $file['name'];
           $type = $file['type'];
           $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$access_token}&type=video";
           $cfile = new CURLFile($path,$type,$name);
           $a = $wechat->http_post($url,["media"=>$cfile],true);//{"type":"image","media_id":"Tld5m4MzOyqqiw0QmQufB85uinfAYsp72AqL2zj_J6FkqkNrfAfwdTY5GopjuIkZ","created_at":1678679960,"item":[]}
           var_dump('video',$a);
           exit;
视频文件不能太大,否则$_FILES会没数据或数据不全.太大需要设置php.ini post_max_size

获取临时文件--image

    //图片返回图片string,需要imagecreatefromstring,video:返回视频url
    $media_id="Tld5m4MzOyqqiw0QmQufB85uinfAYsp72AqL2zj_J6FkqkNrfAfwdTY5GopjuIkZ";
    $media_id="433-MT--lSf3Y1sf5-Jls5BfhHARjaxtmLO5W7eX9sjLdHThwIaYNR18VRNJrIpf";
    $media_id="306IQNnd10EseTH8WuI8T0qiR3oP7vAV5kF0iZAig9jvWlpPQBQaCIxC3aih6nmT";
    $url="https://api.weixin.qq.com/cgi-bin/media/get?access_token={$access_token}&media_id={$media_id}";
    $a = $wechat->http_get($url);
    $im = imagecreatefromstring($a);
    header('Content-Type: image/jpeg');
    //imagejpeg($im,'hahaha.png');//输出到文件
    imagejpeg($im);//浏览器输出

获取临时文件--视频

   $media_id="306IQNnd10EseTH8WuI8T0qiR3oP7vAV5kF0iZAig9jvWlpPQBQaCIxC3aih6nmT";
    $url="https://api.weixin.qq.com/cgi-bin/media/get?access_token={$access_token}&media_id={$media_id}";
    $a = $wechat->http_get($url);
//获取视频 -返回url

上传永久素材--image

   $url= "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={$access_token}&type=image";
        $file = $_FILES['file'];
        $path = $file['tmp_name'];
        $name = $file['name'];
        $type = $file['type'];
        $cfile = new CURLFile($path,$type,$name);
        $a = $wechat->http_post($url,["media"=>$cfile,"description"=>$desc],true);//{"type":"image","media_id":"Tld5m4MzOyqqiw0QmQufB85uinfAYsp72AqL2zj_J6FkqkNrfAfwdTY5GopjuIkZ","created_at":1678679960,"item":[]}

上传永久素材--video

   
        $url= "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={$access_token}&type=video";
        $file = $_FILES['file'];
        $path = $file['tmp_name'];
        $name = $file['name'];
        $type = $file['type'];
        $cfile = new CURLFile($path,$type,$name);
        $desc = json_encode(["title"=>$name,"introduction"=>"hhhhhhdesc"]);//video 需要
        $a = $wechat->http_post($url,["media"=>$cfile,"description"=>$desc],true);//{"media_id":"viTTPko0D5vkS9ovukgJedcTr6eR0edQL2RE0npGylQyD7dhbWNsjSwcnJyqQXeJ","item":[]}

http_post

/**
	 * POST 请求
	 * @param string $url
	 * @param array $param
	 * @param boolean $post_file 是否文件上传
	 * @return string content
	 */
	 function http_post($url,$param,$post_file=false){
		$oCurl = curl_init();
		if(stripos($url,"https://")!==FALSE){
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
		}
		if (is_string($param) || $post_file) {
			$strPOST = $param;
		} else {
			$aPOST = array();
			foreach($param as $key=>$val){
				$aPOST[] = $key."=".urlencode($val);
			}
			$strPOST =  join("&", $aPOST);
		}
		curl_setopt($oCurl, CURLOPT_URL, $url);
		curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt($oCurl, CURLOPT_POST,true);
		curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
		$sContent = curl_exec($oCurl);
		$aStatus = curl_getinfo($oCurl);
		curl_close($oCurl);
		if(intval($aStatus["http_code"])==200){
			return $sContent;
		}else{
			return false;
		}
	}

上传图文消息内的图片获取URL

$url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={$access_token}";
$imgUrl ="https://pic.xxx.com//news/2023/04/11/031502yvbwu79m81lfhrwj.png!standard";
$bodys = array('media' => new CURLFile($imgUrl));
$a = $wechat->http_post($url,$bodys,true);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值