AI智能回复搜索中,请稍后...
2 回答
public function curl_post($url, $data, $header = array()){
if(function_exists('curl_init')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
if(is_array($header) && !empty($header)){
$set_head = array();
foreach ($header as $k=>$v){
$set_head = "$k:$v";
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $set_head);
}
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);// 1s to timeout.
$response = curl_exec($ch);
if(curl_errno($ch)){
//error
return curl_error($ch);
}
$reslut = curl_getinfo($ch);
print_r($reslut);
curl_close($ch);
$info = array();
if($response){
$info = json_decode($response, true);
}
return $info;
} else {
throw new Exception('Do not support CURL function.');
}
}
更改upload_file 方法public function upload_file($file, $type)
{
$app_id = get_setting('weixin_app_id');
$app_secret = get_setting('weixin_app_secret');
$file = realpath($file);
if (!is_readable($file))
{
return false;
}
$file_md5 = md5_file($file);
$cached_result = AWS_APP::cache()->get('weixin_upload_file_' . $file_md5);
if ($cached_result)
{
return $cached_result;
}
$data = array(
'media'=> new CURLFile($file)
);
$result = $this->curl_post(self::WEIXIN_API . 'media/upload?access_token=' . $this->get_access_token($app_id, $app_secret) . '&type=' . $type ,$data);
if (!$result)
{
return false;
}
$result = json_decode($result, true);
if ($result['errcode'])
{
if ($result['errcode'] == 40001)
{
$this->refresh_access_token($app_id, $app_secret);
return $this->upload_file($file, $type);
}
}
else
{
AWS_APP::cache()->set('weixin_upload_file_' . $file_md5, $result, 259200);
}
return $result;
}