新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
//curl
创新互联公司专注于企业营销型网站、网站重做改版、金昌网站定制设计、自适应品牌网站建设、H5技术、商城建设、集团公司官网建设、外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为金昌等各大城市提供网站开发制作服务。
protected function https_CURL($url, $data = null) {
$curl = curl_init();
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_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$list_url);
curl_setopt($ch, CURLOPT_REFERER,$list_urled);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0); //是否显示头文件
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); //上传属性
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); //cookie存放的文件夹
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //是否流
curl_setopt($ch, CURLOPT_PROXY, '120.9.127.1:6675'); //使用代理
curl_setopt($ch, CURLOPT_VERBOSE,1); //出错提示
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"); //模拟浏览器
curl_setopt($ch, CURLOPT_NOBODY,true); //指定了curl抓的内容中包含header头,并且不要body内容
curl_exec($ch);
里面的$post_data就是你要post的上传的数据内容
希望对你能有所帮助。
在调用公众号接口".$token."type=".$type;
上传永久素材文件总是返回 "{\"errcode\":41005,\"errmsg\":\"media data missing\"}"
经过多次测试使用下面的方式,可以正常上传
//调用测试
protected static $url;
protected static $delimiter;
protected static $instance;
public function index()
{
static::$delimiter = uniqid();
$basename = Request::instance()-root();
if (pathinfo($basename, PATHINFO_EXTENSION) == 'php') {
$basename = dirname($basename);
}
$result=$this-wxAddMaterial($token,$basename.'/upload/images/gnlog.jpg','image');
}
// 新增其他类型永久素材
public function wxAddMaterial($token,$filename='',$type='') {
// 设置请求参数
static::$url = "".$token."type=".$type;
$filePath = str_replace('\\', '/', $filename);
// 发送请求
$imginfo=pathinfo($filePath);
$fields = array(
'media'=file_get_contents(".".$filePath),
'filename'=$imginfo["basename"],
);
$res = $this-putPart( $fields);
// 发送请求
return $res;
}
//推送文件流
public function putPart($param) {
$post_data = static::buildData($param);
$curl = curl_init(static::$url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
"Content-Type: multipart/form-data; boundary=" . static::$delimiter,
"Content-Length: " . strlen($post_data)
]);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
//编译请求头格式和数据流
private static function buildData($param){
$data = '';
$eol = "\r\n";
$upload = $param['media'];
unset($param['media']);
foreach ($param as $name = $content) {
$data .= "--" . static::$delimiter . "\r\n"
. 'Content-Disposition: form-data; name="' . $name . "\"\r\n\r\n"
. $content . "\r\n";
}
$data .= "--" . static::$delimiter . $eol
. 'Content-Disposition: form-data; name="media"; filename="' . $param['filename'] . '"' . "\r\n"
. 'Content-Type:application/octet-stream'."\r\n\r\n";
$data .= $upload . "\r\n";
$data .= "--" . static::$delimiter . "--\r\n";
return $data;
}
根据自己的实际情况稍作修改
$Data = array('user' = 'xiaoming');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,'');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $Data);
$a = curl_exec($ch);
curl_close($ch);
print_r( $a );