微信获取下载临时素材(图片)

curl 获取文件数据

	function curl_file($url){
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_NOBODY, 0);                //只取body头
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//curl_exec执行成功后返回执行的结果;不设置的话,curl_exec执行成功则返回true
		$output = curl_exec($ch);
		curl_close($ch);
		return $output;
	}

	//保存文件到本地
	function saveFile($filename, $filecontent){
		$local_file = fopen($filename, 'w');
		if (false !== $local_file){//不恒等于(恒等于=== 就是false只能等于false,而不等于0)
			if (false !== fwrite($local_file, $filecontent)) {
				fclose($local_file);
			}
		}
	}

        //获取并保存
	public function getTmpImg2()
	{
		$token = cacheToken();                                                                                                                        //我们获取token得方法
		$media_id = 's4kUVV5ZbRqvhOvtfarHMSM_F6qLvg9e_WR9TiOfnmtMKQjgoVlbKN2kTe4LqBuW';   //临时素材id
		$url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=$token&media_id=$media_id";      
		$arr = $this->curl_file($url);
		$this->saveFile("newSmallelife.jpg",$arr);
	}
posted @ 2017-11-13 19:20  夜啸苍姬  阅读(3023)  评论(0编辑  收藏  举报