网站首页 think技术
微信CURL上传图片素材--永久图片素材
发布时间:2016-06-08 03:36查看次数:3258
<?php // 这个是腾讯要求的数据格式 $filedata = array(); $filedata['filename'] = '2.jpg'; //数据名字 $filedata['content-type'] = 'image/jpeg'; //类型 有PNG JPEG $filedata['filelength'] = '38051'; //文件长度 $token1 = "DE-93dDsetddNXAIXVYQRlk0-vG2gKq-mSq2jkkPy1QJWsOueGUoJoLrQ5r82qHWhdXrF96j2k1ZB-Q_cqpc68ItR2twC7iv5lb_g8s_BZOkZlFNp_rY13DG9SDJxt7BXAOcAFAHKY"; $tmp= add_material($token1,$filedata); var_dump($tmp); function add_material($token, $file_info){ $access_token = $token; // $url="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={$access_token}&type=image";//永久素材 $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$access_token}&type=image"; //临时素材 $ch = curl_init (); $timeout = 5; $real_path= __dir__.'\\'."{$file_info['filename']}"; //取得图片在机器中的绝对路径 $data= array("media"=>"@{$real_path}");//,'form-data'=>$file_info curl_setopt ( $ch, CURLOPT_URL, $url ); curl_setopt ( $ch, CURLOPT_POST, 1 ); //使用普通POST模式 curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, $timeout ); //超时时间 curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE ); curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, false ); @curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data); $result = curl_exec ( $ch ); curl_close ( $ch ); if(@curl_errno($result)==0){ return json_decode($result,true); }else { return false; } } ?>
关键字词:微信CURL上传图片素材--永久图片素材