<?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>

以下是网上看到的另外的代码

最近需要用http模拟表单提交,简单的数据提交倒是简单,如果要上传图片就要稍微做些变动了。下面分享一下,利用curl和fsockopen两种方法实现,我测试通过的例子。

发送请求的页面:fsocketupload.PHP

<?php
/**
* @author zhao jinhan
* @email: xb_zjh@126.com
* @url : www.icode100.com
* @date : 2014年1月18日13:09:42
*
*/
header('Content-type:text/html; charset=utf-8'); //声明编码
//模拟POST上传图片和数据
//第一种方法:CURL
$ch = curl_init();
$url = 'http://www.test.localhost/fsocketget.php';
$curlPost = array('sid'=>1,'file'=>'@D:/xampp/htdocs/www/www.test.localhost/images/adding.gif');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1); //POST提交
curl_setopt($ch, CURLOPT_POSTFIELDS,$curlPost);
$data =curl_exec($ch);
curl_close($ch);
echo '<pre>';
var_dump($data); //第二种方法:fsockopen方法上传 $arr_data = array('sid'=>1,'mid'=>2); //普通参数
$var_file='image'; //文件变量名
$file_type='image/jpeg'; //文件类型
$filepath = 'D:/xampp/htdocs/www/www.test.localhost/images/2.jpg'; //文件路径
$filestring = @file_get_contents($filepath) or exit('not found file ( '.$filepath.' )'); //生成文件流
$host = 'www.test.localhost'; //构造post请求的头
$boundary = substr(md5(time()),8,16); //分隔符
$header = "POST /fsocketget.php HTTP/1.1\r\n";//一般有post, get这两种
$header .= "Host: {$host}\r\n";
$header .= "Content-Type: multipart/form-data; boundary={$boundary}\r\n"; $data = "";
//请求普通数据
foreach($arr_data as $k=>$v){
$data .= "--{$boundary}\r\n";
$data .= "Content-Disposition: form-data; name=\"{$k}\"\r\n";
$data .= "\r\n{$v}\r\n";
$data .= "--{$boundary}\r\n";
}
//请求图片数据
$filename = basename($filepath); //文件名
$data .= "--{$boundary}\r\n";
$data .= "Content-Disposition: form-data; name=\"$var_file\"; filename=\"$filename\"\r\n";
$data .= "Content-Type: $file_type\r\n"; //\r\n不可少
$data .= "\r\n$filestring\r\n"; //\r\n不可少
$data .= "--{$boundary}\r\n"; //\r\n不可少
$header .= "Content-Length: ".strlen($data)."\r\n\r\n"; //\r\n不可少 //发送post的数据
$fp = fsockopen($host,80,$errno,$errstr,10) or exit($errstr."--->".$errno);
fputs($fp,$header.$data); $inheader = 0; //1去除请求包的头只显示页面的返回数据 0-保留头
while (!feof($fp)) {
$line = fgets($fp,1024);
if ($inheader && ($line == "\n" || $line == "\r\n")) {
$inheader = 0;
}
if ($inheader == 0) {
echo $line;
}
} fclose($fp);

请求api页面:fsocketget.php

<?php
/* 接收请求的API */
if($_POST){
echo '<pre>';
print_r($_POST);
print_r($_FILES);
}else{
echo 'error';
}

最新文章

  1. Tomcat server.xml配置示例
  2. winform 多个label绑定一个事件
  3. MS SQL数据类型比较
  4. select 选项的子句
  5. OS——进程简答题(1)
  6. POJ3096Surprising Strings(map)
  7. L - Kakuro Extension - HDU 3338 - (最大流)
  8. Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
  9. 深入理解 JavaScript 异步系列(3)—— ES6 中的 Promise
  10. 在H3C交换机上开通一个VLAN并且开通一个端口ping通它
  11. 算法第四版 coursera公开课 普林斯顿算法 ⅠⅡ部分 Robert Sedgewick主讲《Algorithms》
  12. vue中的vue-cli
  13. Node.js 串口通讯 node-serialport 使用说明
  14. Educational Codeforces Round 30 D. Merge Sort
  15. 金蝶PDA金蝶盘点机金蝶仓库条码管理方案-采购入库单教程
  16. Ubuntu/Debian apt-get 404 Not Found Package Repository Errors,无法找到包的错误
  17. MySQL版本升级参考资料【转】
  18. ARM linux内核启动时几个关键地址【转】
  19. 微信小程序——自定义图标组件
  20. centos7 开启ftp服务

热门文章

  1. checkbox-padding 调整checkbox字体跟图标距离
  2. elasticsearch的master选举机制
  3. Android 通过局域网udp广播自动建立socket连接
  4. golang pipe
  5. 关于EJB--实体Bean的BMP和CMP选择
  6. BZOJ1492: [NOI2007]货币兑换Cash(CDQ分治,斜率优化动态规划)
  7. 含有过滤功能的android流式布局
  8. WebStorm(Amaze开发工具)--JavaScript 开发工具
  9. js面向对象1----了解构造函数
  10. 利用ServiceWorker实现页面的快速加载和离线访问