/** ????* 跨域post请求(适配文件上传) ????* @param $url 上传地址 ????* @param $post_data 请求数据 ????*/ ????protected function sendPost ($url,$post_data) ???{ ???????/*$url = "http://10.0.0.87/fileAccept.php"; ???????$post_data = array( ???????????"dir" => "wechat", ???????????//要上传的本地文件地址 ???????????"upload" => ‘@/alidata/www/15c56d316166c1fccfb036a9337d0caeChrysanthemum.jpg‘ ???????);*/ ???????$ch = curl_init(); ???????curl_setopt($ch , CURLOPT_URL , $url); ???????curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1); ???????curl_setopt($ch , CURLOPT_POST, 1); ???????curl_setopt($ch , CURLOPT_POSTFIELDS, $post_data); ???????$output = curl_exec($ch); ???????curl_close($ch); ???????return $output; ???}
接收:
<?php /*** 跨域文件接收*/class fileAccept{protected $dis = ‘/alidata/www/cdnappota/‘; protected $ur = ‘http://cdn.multiopen.cn/‘;// Function to get the client IP addressfunction get_client_ip() { ???$ipaddress = ‘‘; ???if (getenv(‘HTTP_CLIENT_IP‘)) ???????$ipaddress = getenv(‘HTTP_CLIENT_IP‘); ???else if(getenv(‘HTTP_X_FORWARDED_FOR‘)) ???????$ipaddress = getenv(‘HTTP_X_FORWARDED_FOR‘); ???else if(getenv(‘HTTP_X_FORWARDED‘)) ???????$ipaddress = getenv(‘HTTP_X_FORWARDED‘); ???else if(getenv(‘HTTP_FORWARDED_FOR‘)) ???????$ipaddress = getenv(‘HTTP_FORWARDED_FOR‘); ???else if(getenv(‘HTTP_FORWARDED‘)) ???????$ipaddress = getenv(‘HTTP_FORWARDED‘); ???else if(getenv(‘REMOTE_ADDR‘)) ???????$ipaddress = getenv(‘REMOTE_ADDR‘); ???else ???????$ipaddress = ‘UNKNOWN‘; ???return $ipaddress;}function uploadFile($files,$dir=‘images‘){if ($files) { $time = time(); ???????$newname = md5(uniqid().$time); ???????if (!file_exists($this->dis.$dir.‘/‘)) { ???????mkdir($this->dis.$dir.‘‘); ???????} ??????$res = ?move_uploaded_file($files[‘tmp_name‘],$this->dis.$dir.‘/‘.$newname.$files[‘name‘]);if ($res) {echo json_encode([‘errno‘=>0,‘data‘=>[$this->ur.$dir.‘/‘.$newname.$files[‘name‘]]]);}else{echo json_encode([‘errno‘=>1]);}}}}$files = $_FILES[‘upload‘];$dir = $_POST[‘dir‘];$acc = new fileAccept();$ip = $acc->get_client_ip();$whiteIp = [‘10.0.0.117‘];if (in_array($ip,$whiteIp)) {echo $acc->uploadFile($files,$dir);}
php <5.5 curl post and file 请求
原文地址:http://www.cnblogs.com/leescre/p/8004207.html