<?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. 使用git进行团队合作开发

    1.git 和 svn 的差异 git和svn 最大的差异在于git是分布式的管理方式而svn是集中式的管理方式.如果不习惯用代码管理工具,可能比较难理解分布式管理和集中式管理的概念.下面介绍两种工具 ...

  2. jqueryEasyUI:tabs扩展:给tabs组件绑定双击事件 分类: JqueryEasyUI 2014-09-29 14:36 537人阅读 评论(0) 收藏

    实现代码: $.extend($.fn.tabs.methods, { /** * 绑定双击事件 * @param {Object} jq * @param {Object} caller 绑定的事件 ...

  3. PHP连接打印机

    <?php header("Content-type: text/html; charset=utf-8"); class Netprint{ public $host = ...

  4. hdu 3236 二维背包

    明天来一发 hdu 4501  算是这题的简化版吧

  5. JS 正则表达式详解

    在此提供相关的链接,请访问: http://www.cnblogs.com/dolphinX/p/3486214.html http://www.cnblogs.com/dolphinX/p/3486 ...

  6. java线程之——synchronized的注意细节

    我在学习synchronized的时候,十分好奇当一个线程进入了一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法? 然后就做了个实验(实验代码最后贴出),最后得到了如下 ...

  7. Linux编程(3) MakeFile

    1. 在Linux中,make工具可以维护程序模块关系和生成可执行程序.它可根据程序模块的修改情况重新编译链接生成中间代码或最终的可执行程序.执行make命令,需要一个名为Makefile的文本文件, ...

  8. 使用 json_in_java

    // */ // ]]> java_in_json Table of Contents 1. Java 使用 Json 1.1. 下载地址: 1.2. 构造 json 字符串 1.3. 解析 j ...

  9. CSS3-给网页添加图片

    给网页添加图片: 1.background-attachment: scroll--------随文本一块滚动 ; background-attachment: fixed-----固定在一个位置上 ...

  10. JS 中面向对象的5种写法

    //第1种写法 function Circle(r) { this.r = r; } Circle.PI = 3.14159; Circle.prototype.area = function() { ...