zoukankan      html  css  js  c++  java
  • php <5.5 curl post and file 请求

    /**
         * 跨域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://域名/';
    	
    	// Function to get the client IP address
    	function 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'];//ip白名单
    if (in_array($ip,$whiteIp)) {
    	echo $acc->uploadFile($files,$dir);
    }
    

      

  • 相关阅读:
    BZOJ5321 JXOI2017加法(二分答案+贪心+堆+树状数组)
    BZOJ5089 最大连续子段和(分块)
    Codeforces 893F(主席树+dfs序)
    BZOJ5092 分割序列(贪心)
    Codeforces Round #525 Div. 2 自闭记
    364. Nested List Weight Sum II
    362. Design Hit Counter
    369. Plus One Linked List
    370. Range Addition
    366. Find Leaves of Binary Tree
  • 原文地址:https://www.cnblogs.com/leescre/p/8004207.html
Copyright © 2011-2022 走看看