分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

php <5.5 curl post and file 请求

发布时间:2023-09-06 01:29责任编辑:林大明关键词:url
/** ????* 跨域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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved