PHP curl 模拟POST 上传文件(含php 5.5后CURLFile)
<?php
/**
* Email net.webjoy@gmail.com
* author jackluo
* 2014.11.21
*
*/ //*
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, 1);// 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.');
}
}
//*/
//
function api_notice_increment($url, $data)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
// $data = http_build_query($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//curl_file_create
// $result = curl_exec($ch);
$lst['rst'] = curl_exec($ch);
$lst['info'] = curl_getinfo($ch);
curl_close($ch); return $lst;
// return $result;
} /**
* curl文件上传
* @var struing $r_file 上传文件的路劲和文件名
*
*/
/*
function upload_file($url,$r_file)
{
$file = array("fax_file"=>'@'.$r_file,'type'=>'image/jpeg');//文件路径,前面要加@,表明是文件上传.
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_POST,1);
curl_setopt($curl,CURLOPT_POSTFIELDS,$file);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
$result = curl_exec($curl); //$result 获取页面信息
curl_close($curl);
echo $result ; //输出 页面结果
}*/ function upload_file($url,$filename,$path,$type){
$data = array(
'pic'=>'@'.realpath($path).";type=".$type.";filename=".$filename
);
$ch = curl_init();
//设置帐号和帐号名
curl_setopt($ch, CURLOPT_USERPWD, 'joe:secret' );
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_getinfo($ch);
$return_data = curl_exec($ch);
curl_close($ch);
echo $return_data;
} // php 5.5 以后请用以下函数
function upload_file($url,$filename,$path,$type){
$data = array(
'pic'=>new CURLFile(realpath($path))
);
$ch = curl_init();
//也可以用以下注释掉的不用改代码,觉得新版的可以省下点代码,看个人
//curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
//设置帐号和帐号名
curl_setopt($ch, CURLOPT_USERPWD, 'joe:secret' );
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_getinfo($ch);
$return_data = curl_exec($ch);
curl_close($ch);
echo $return_data;
}
if ($_POST) {
$url = 'http://platform.com/upload/image';
//
$path = $_SERVER['DOCUMENT_ROOT'];
/*
print_r($_FILES);
exit;
*/
//$filename = $path."/232.jpg";
//upload tmp
$tmpname = $_FILES['fname']['name'];
$tmpfile = $_FILES['fname']['tmp_name'];
$tmpType = $_FILES['fname']['type'];
// echo $tmpType;
upload_file($url,$tmpname,$tmpfile,$tmpType);
/*
$data = array(
'path'=>"@$path/232.jpg",
'name'=>'h'
);
*/
//'pic'=>'@/tmp/tmp.jpg', 'filename'=>'tmp'
//$data = array('pic'=>"@$filename", 'filename'=>'tmp');
/*
$data = array(
'uid' => 10086,
'pic' => '@$tmpfile'.';type='.$tmpType
);
$info = api_notice_increment($url, $data);
*/
//$info = curl_post($url, $data);
//$info = api_notice_increment($url, $data);
//upload_file($url,$tmpfile);
//print_r($info);
exit;
/*
$file = 'H:\www\test\psuCARGLSPA-pola.jpg'; //要上传的文件
$src = upload_curl_pic($file);
echo $src;
*/
}
?> <form action="http://localhost/upload.php" enctype="multipart/form-data" method="post">
<p>UpLoad: <input type="text" name="fname" /></p>
<p>UpLoad: <input type="file" name="fname" /></p> <input type="submit" value="Submit" />
</form>
PHP curl 模拟POST 上传文件(含php 5.5后CURLFile)的更多相关文章
- 通过PHP CURL模拟请求上传文件|图片。
现在有一个需求就是在自己的服务器上传图片到其他服务器上面,过程:客户端上传图片->存放到本地服务器->再转发到第三方服务器; 由于前端Ajax受限制,只能通过服务器做转发了. 在PHP中通 ...
- PHP通过curl模拟POST上传文件,5.5之前和之后的区别
首先先要着重提一下,只要是做和项目有关的开发,首先按把环境中各个服务的版本保持一致,否则出些莫名其妙的错我,让你百爪挠心却不知哪里的问题.这里就要说下curl_setopt($ch, CURLOPT_ ...
- Java模拟http上传文件请求(HttpURLConnection,HttpClient4.4,RestTemplate)
先上代码: public void uploadToUrl(String fileId, String fileSetId, String formUrl) throws Throwable { St ...
- ApiPost接口调试工具模拟Post上传文件(中文版Postman)
ApiPost简介: ApiPost是一个支持团队协作,并可直接生成文档的API调试.管理工具.它支持模拟POST.GET.PUT等常见请求,是后台接口开发者或前端.接口测试人员不可多得的工具 . A ...
- c# 模拟POST上传文件到服务器
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...
- PHP中curl模拟post上传及接收文件
public function Action_Upload(){ $this->path_config(); exit(); $furl="@d:\develop\JMFramewor ...
- Linux 基础命令-CURL 表单上传文件
CURL -F, --form <name=content> (HTTP) This lets curl emulate a filled-in form in which a user ...
- 通过WebClient模拟post上传文件到服务器
写在前面 最近一直在研究sharepoint的文档库,在上传文件到文档库的过程中,需要模拟post请求,也查找了几种模拟方式,webclient算是比较简单的方式. 一个例子 这里写一个简单接受pos ...
- 用iFrame模拟Ajax上传文件
前段时间在解决ajax上传文件时折腾了好一阵.直接用$.post上传文本信息肯定是没有问题的.但是$.post直接上传图片是不可行的. 后来看到网上的一些解决方案,有现成的ajax上传文件的封装的方法 ...
随机推荐
- BI 项目管理之生命周期跟踪和任务区域
DW/BI 系统是复杂的实体,构建这种系统的方法必须有助于简化复杂性.13 个方框显示了构建成功的数据仓库的主要任务区域,以及这些任务之间的主要依赖关系. 在生命周期这一级可以进行多方观察 ...
- 使用windows的远程桌面连接连接Ubuntu
想起来用笔记本连接一个windows server时只需要在远程桌面连接里面输入一下ip地址然后账号密码就可以了,十分简单.于是乎既然装了个Ubuntu当服务器使那么我就业来远程连接一下,由于wind ...
- Alcatraz安装 不能用解决方案
1.安装 1>Github上下载Alcatraz,下载地址:https://github.com/supermarin/Alcatraz 2>Alcatraz是xcode的插件,这个插件 ...
- slide.js使用文档
<!doctype html> <head> <script src="js/jquery-latest.min.js"></script ...
- Android开发工具之Dash
作为一名死coder,每天最常见的动作就是查看各种API文档,你一定也有过同时打开N个窗口(HTML.PDF.CHM),不停的在编辑器与文档之间切换的感受吧?怎么说呢,其实我很讨厌这种枯燥无味的动作, ...
- 数据库ORM框架GreenDao
常用的数据库: 1). Sql Server2). Access3). Oracle4). Sysbase5). MySql6). Informix7). FoxPro8). PostgreSQL9) ...
- 完善SQL农历转换函数
-------------------------------------------------------------------- -- Author : 原著: 改编:ht ...
- try : finally语句
try:finally语句不管有没有异常他都会执行:他就是用来清理的try: h=open("ll","r") y=h.read() print (int(y) ...
- vim使用02
编辑 剪切光标所在的字符: <x>; 剪切并插入: <s> 撤销操作:撤销至上一个命令之间的修改: <u> 恢复上一次撤销操作: <C r> 剪切光标所 ...
- 滑雪(简单dp)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 81099 Accepted: 30239 Description Mic ...