php http模拟POST提交上传图片

转自:http://www.icode100.com/posts/view/66

最近需要用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';
}



转自:http://www.icode100.com/posts/view/66

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值