公众号新增临时素材返回空
$url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=".$token."&type=image";
if(class_exists('CURLFile')){
$bodys = array('media' => new \CURLFile(realpath($img_url)));
}else{
$bodys = array('media' => '@'.$img_url);
}
$res = self::curl_post($url, $bodys);
var_dump($res); //此处返回了一个空的字符串
public static function curl_post($url, $postdata ) {
//初始化
$curl = curl_init();
if(class_exists('CURLFile')){
curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
}else{
if(defined('CURLOPT_SAFE_UPLOAD')){
curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
}
}
//设置抓取的url
curl_setopt($curl, CURLOPT_URL, $url);
//设置头文件的信息作为数据流输出
curl_setopt($curl, CURLOPT_HEADER, 0);
//设置获取的信息以文件流的形式返回,而不是直接输出。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// 超时设置
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE );
//设置post方式提交
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
//执行命令
$data = curl_exec($curl);
// 显示错误信息
if (curl_error($curl)) {
return curl_error($curl);
} else {
curl_close($curl);
return $data;
}
}
解决了吗?
你好,问题未解决的话,请补充完整的请求,access_token也要如实填写,也补充请求时间
请求api:https://api.weixin.qq.com/cgi-bin/media/upload?type=image
请求时间:2020-3-9 11:16
access_token:31_O4RhKXhdQ8GFh0pJwtUUJeVGwQa6qcLC1FvYJl8wx7pn8kQWO_vfhJZnXQEYBIjiKNMVFiouV22Tymg7eCmQofMDcHUigMF2tMwmpSQ2GLfBp295fBia9J2KnhizNfOjZFF65aRNyx1o7srCLFJdAEATNR
请求url: "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=".$token."&type=image"
$token = self::getToken();
//1.添加临时素材
$url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=".$token."&type=image";
if(class_exists('CURLFile')){
$bodys = array('media' => new \CURLFile(realpath($img_url)));
}else{
$bodys = array('media' => '@'.$img_url);
}
$res = self::curl_post($url, $bodys);
var_dump($res);
}else{
// 超时设置,以毫秒为单位
// curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);
// 设置请求头
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE );
//设置post方式提交
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
//执行命令
$data = curl_exec($curl);
var_dump($data);
// 显示错误信息
if (curl_error($curl)) {
// var_dump(curl_error($curl));
print "Error: " . curl_error($curl);
return "Error: " . curl_error($curl);
} else {
// 打印返回的内容
// var_dump($data);
curl_close($curl);
return $data;
}
}
$url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=".$token."&type=image";
app id wx3a3f8883b1354943