-
-
-
-
header( 'content-type:text/html;charset=utf-8');
-
error_reporting( E_ALL ^ E_NOTICE);
-
function CurlPost($url, $param = [], $is_Post = 1 ,$timeout = 10)
-
{
-
-
-
//初始化curl
-
$curl = curl_init();
-
-
-
curl_setopt($curl, CURLOPT_URL, $url); // 设置请求的路径
-
-
-
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
-
-
-
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST, false);
-
if( $is_Post ){
-
-
-
curl_setopt($curl, CURLOPT_POST, 1); //设置POST提交
-
-
-
curl_setopt($curl,CURLOPT_SAFE_UPLOAD , true );
-
-
-
//提交数据
-
if (is_array($param)) {
-
#不能使用http_bulid_query
-
curl_setopt($curl, CURLOPT_POSTFIELDS, ($param));
-
// @curl_setopt($curl, CURLOPT_POSTFIELDS, ($param));
-
} else {
-
curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
-
}
-
}
-
-
-
-
-
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //显示输出结果
-
-
-
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
-
-
-
-
-
//执行请求
-
$data = $data_str = curl_exec($curl);
-
-
-
//处理错误
-
if ($error = curl_error($curl)) {
-
$logdata = array(
-
'url' => $url,
-
'param' => $param,
-
'error' => '<span style="color:red;font-weight: bold">' . $error . '</span>',
-
);
-
-
-
var_dump($logdata);
-
exit;
-
}
-
-
-
-
-
curl_close($curl);
-
-
-
//json数据转换为数组
-
$data = json_decode($data, true);
-
-
-
if (!is_array($data)) {
-
$data = $data_str;
-
}
-
-
-
return $data;
-
-
-
}
-
-
$token = 'L3VWbz5oaUc5LS-3pTmso5Sa3GW4BqPZ3bMiz4Zy0poixY4OYrlF-S_V4jmQ5EYNaJzYXTUuc3fS8KLtPIYfAKplq4AtrAwKFCxlGSwlq2gOVZhAAAJQU';
-
-
-
# 临时素材的接口
-
$upload_url = 'https://api.weixin.qq.com/cgi-bin/media/upload?access_token='.$token. '&type=image';
-
-
-
$cfile = curl_file_create( 'C:\wamp\www\weixin\a.jpg');
-
-
-
$data = array(
-
// 'media'=>$cfile,
-
// 'media'=>'@'.realpath('a.jpg'),
-
'media'=> new CURLFile( 'C:\wamp\www\weixin\a.jpg'),
-
'form-data' => [
-
'filename' => 'a.jpg',
-
'filelength'=> 1839,
-
'content-type'=> 'image/jpeg'
-
]
-
-
-
);
-
-
$result = CurlPost( $upload_url , $data );
-
-
-
var_dump($result);
-
exit;
1、微信接口access_token获取失败
直接在阿里云服务器上curl可以可以获取到token,在命令行直接php ./test.php也可以。
问题出在php的curl扩展没有安装
2、关于上传永久素材时,提示40015.
问题在于php 5.6之后CURL不允许使用 @上传问题,可以使用 new CURLFILE('文件代替')或者 curl_create_file代替
-
-
-
-
header( 'content-type:text/html;charset=utf-8');
-
error_reporting( E_ALL ^ E_NOTICE);
-
function CurlPost($url, $param = [], $is_Post = 1 ,$timeout = 10)
-
{
-
-
-
//初始化curl
-
$curl = curl_init();
-
-
-
curl_setopt($curl, CURLOPT_URL, $url); // 设置请求的路径
-
-
-
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
-
-
-
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST, false);
-
if( $is_Post ){
-
-
-
curl_setopt($curl, CURLOPT_POST, 1); //设置POST提交
-
-
-
curl_setopt($curl,CURLOPT_SAFE_UPLOAD , true );
-
-
-
//提交数据
-
if (is_array($param)) {
-
#不能使用http_bulid_query
-
curl_setopt($curl, CURLOPT_POSTFIELDS, ($param));
-
// @curl_setopt($curl, CURLOPT_POSTFIELDS, ($param));
-
} else {
-
curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
-
}
-
}
-
-
-
-
-
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //显示输出结果
-
-
-
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
-
-
-
-
-
//执行请求
-
$data = $data_str = curl_exec($curl);
-
-
-
//处理错误
-
if ($error = curl_error($curl)) {
-
$logdata = array(
-
'url' => $url,
-
'param' => $param,
-
'error' => '<span style="color:red;font-weight: bold">' . $error . '</span>',
-
);
-
-
-
var_dump($logdata);
-
exit;
-
}
-
-
-
-
-
curl_close($curl);
-
-
-
//json数据转换为数组
-
$data = json_decode($data, true);
-
-
-
if (!is_array($data)) {
-
$data = $data_str;
-
}
-
-
-
return $data;
-
-
-
}
-
-
$token = 'L3VWbz5oaUc5LS-3pTmso5Sa3GW4BqPZ3bMiz4Zy0poixY4OYrlF-S_V4jmQ5EYNaJzYXTUuc3fS8KLtPIYfAKplq4AtrAwKFCxlGSwlq2gOVZhAAAJQU';
-
-
-
# 临时素材的接口
-
$upload_url = 'https://api.weixin.qq.com/cgi-bin/media/upload?access_token='.$token. '&type=image';
-
-
-
$cfile = curl_file_create( 'C:\wamp\www\weixin\a.jpg');
-
-
-
$data = array(
-
// 'media'=>$cfile,
-
// 'media'=>'@'.realpath('a.jpg'),
-
'media'=> new CURLFile( 'C:\wamp\www\weixin\a.jpg'),
-
'form-data' => [
-
'filename' => 'a.jpg',
-
'filelength'=> 1839,
-
'content-type'=> 'image/jpeg'
-
]
-
-
-
);
-
-
$result = CurlPost( $upload_url , $data );
-
-
-
var_dump($result);
-
exit;
1、微信接口access_token获取失败
直接在阿里云服务器上curl可以可以获取到token,在命令行直接php ./test.php也可以。
问题出在php的curl扩展没有安装
2、关于上传永久素材时,提示40015.
问题在于php 5.6之后CURL不允许使用 @上传问题,可以使用 new CURLFILE('文件代替')或者 curl_create_file代替