<?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)的更多相关文章

  1. 通过PHP CURL模拟请求上传文件|图片。

    现在有一个需求就是在自己的服务器上传图片到其他服务器上面,过程:客户端上传图片->存放到本地服务器->再转发到第三方服务器; 由于前端Ajax受限制,只能通过服务器做转发了. 在PHP中通 ...

  2. PHP通过curl模拟POST上传文件,5.5之前和之后的区别

    首先先要着重提一下,只要是做和项目有关的开发,首先按把环境中各个服务的版本保持一致,否则出些莫名其妙的错我,让你百爪挠心却不知哪里的问题.这里就要说下curl_setopt($ch, CURLOPT_ ...

  3. Java模拟http上传文件请求(HttpURLConnection,HttpClient4.4,RestTemplate)

    先上代码: public void uploadToUrl(String fileId, String fileSetId, String formUrl) throws Throwable { St ...

  4. ApiPost接口调试工具模拟Post上传文件(中文版Postman)

    ApiPost简介: ApiPost是一个支持团队协作,并可直接生成文档的API调试.管理工具.它支持模拟POST.GET.PUT等常见请求,是后台接口开发者或前端.接口测试人员不可多得的工具 . A ...

  5. c# 模拟POST上传文件到服务器

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  6. PHP中curl模拟post上传及接收文件

    public function Action_Upload(){ $this->path_config(); exit(); $furl="@d:\develop\JMFramewor ...

  7. Linux 基础命令-CURL 表单上传文件

    CURL -F, --form <name=content> (HTTP) This lets curl emulate a filled-in form in which a user ...

  8. 通过WebClient模拟post上传文件到服务器

    写在前面 最近一直在研究sharepoint的文档库,在上传文件到文档库的过程中,需要模拟post请求,也查找了几种模拟方式,webclient算是比较简单的方式. 一个例子 这里写一个简单接受pos ...

  9. 用iFrame模拟Ajax上传文件

    前段时间在解决ajax上传文件时折腾了好一阵.直接用$.post上传文本信息肯定是没有问题的.但是$.post直接上传图片是不可行的. 后来看到网上的一些解决方案,有现成的ajax上传文件的封装的方法 ...

随机推荐

  1. [Eclipse] eclipse中打开xml文件,使用ctrl+鼠标左键无法跳转至Java源文件【待解决】

    eclipse中打开xml文件,使用ctrl+鼠标左键无法跳转至Java源文件: 1. 设置eclipse ctrl + 左键打开源文件代码,如下图,设置都正常 2. 在网上找了很多种办法,均失败,在 ...

  2. [Java] 使用Java Visual VM寻找PermGen Space的解决办法

    在Eclipse使用tomcat运行3个项目时,老是报这个错误,以下为错误详情: 2014-5-28 13:47:41 org.apache.catalina.core.StandardWrapper ...

  3. (转载)一个用于Gnome桌面的下拉式终端: Guake 0.7.0 发布

    转自:https://linux.cn/article-5507-1.html Linux的命令行是最好.最强大的东西,它使新手着迷,并为老手和极客的提供极其强大的功能.那些在服务器和生产环境下工作的 ...

  4. OGNL表示式使用和值栈

    另外值得参考博客:http://blog.csdn.net/resigshy/article/details/7560573 OGNL是Object Graphic Navigation Langua ...

  5. XStream-----把JavaBean转换为xml的工具

    1. 什么作用 可以把JavaBean转换为(序列化为)xml 2. XStream的jar包 核心JAR包:xstream-1.4.7.jar: 必须依赖包:xpp3_min-1.1.4c(XML ...

  6. LoadRunner中循环操作

    Action() {     //Loadrunner中的FOR,WHILE,DO 循环语句 int i;   int whileloop = 1;   //FOR 循环   for (i=1;i&l ...

  7. XSS攻击&SQL注入攻击&CSRF攻击?

    - XSS(Cross Site Script,跨站脚本攻击)是向网页中注入恶意脚本在用户浏览网页时在用户浏览器中执行恶意脚本的攻击方式.跨站脚本攻击分有两种形式:反射型攻击(诱使用户点击一个嵌入恶意 ...

  8. 02 CSS/javaScript

    CSS(Cascading Style Sheets)是层叠样式表用来设置网页的显示效果.可以解决html代码对样式定义的重复,提高了后期样式代码的可维护性,并增强了网页的显示效果功能.简单一句话:C ...

  9. 01 HTML基础

    HTML就是超文本标记语言的简写,是最基础的网页语言. 通过标签定义的语言,代码都是由标签所组成的.(最重要的标签是定义标题.段落和换行的标签) 不区分大小写 head部分是给html页面增加一些辅助 ...

  10. Fzu月赛11 老S的旅行计划 dij

    Description 老S在某城市生活的非常不自在,想趁着ICPC举办期间在省内转转.已知老S所在的省有N个城市,M条无向边(对于某一对结点可能出现重边).由于省内的交通相当糟糕,通过某条边所需要花 ...