不是图片的地址,是图片本身,需要用 fopen 打开:
public function uploadPhoto($img)
{
$args['mch_id'] = $this->mch_id;
$args['media_hash'] = md5_file($img);
$args['sign_type'] = 'HMAC-SHA256';
$args['sign'] = $this->getSign($args);
$args['media'] = fopen($img, 'r');
$i = 0;
foreach ($args as $key => $item) {
$data[$i]['name'] = $key;
$data[$i]['contents'] = $item;
$i++;
}
//发送参数
$url = "
https://api.mch.weixin.qq.com/secapi/mch/uploadmedia";
$res = $this->request('POST', $url, ['cert' => $this->apiclient_cert, 'ssl_key' => $this->apiclient_key, 'verify' => false, 'multipart' => $data]);
$ret = $this->xmlToArray($res);
if ($ret['return_code'] != 'SUCCESS') {
return false;
}
return $ret['media_id'];
}