考试首页 | 考试用书 | 培训课程 | 模拟考场 | 考试论坛  
  当前位置:编程开发 > PHP > 文章内容
  

PHP教程:php利用fsockopenGET/POST提交表单及上传文件

 [ 2017年9月4日 ] 【

  php利用fsockopen GET/POST提交表单及上传文件,具体内容如下

  1.GET

  get.php

  ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
$host = 'demo.fdipzone.com';
$port = 80;
$errno = '';
$errstr = '';
$timeout = 30;
$url = '/socket/getapi.php';
  
$param = array(
  'name' => 'fdipzone',
  'gender' => 'man'
);
  
$url = $url.'?'.http_build_query($param);
  
// create connect
$fp = fsockopen($host, $port, $errno, $errstr, $timeout);
  
if(!$fp){
  return false;
}
  
// send request
$out = "GET ${url} HTTP/1.1\r\n";
$out .= "Host: ${host}\r\n";
$out .= "Connection:close\r\n\r\n";
  
fputs($fp, $out);
  
// get response
$response = '';
while($row=fread($fp, 4096)){
  $response .= $row;
}
  
fclose($fp);
  
$pos = strpos($response, "\r\n\r\n");
$response = substr($response, $pos+4);
  
echo $response;
?>

  getapi.php

  ?

1
2
3
4
5
6
7
$name = $_GET['name'];
$gender = $_GET['gender'];
  
echo 'name='.$name.'
'
;
echo 'gender='.$gender;
?>

  2.POST

  post.php

  ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
$host = 'demo.fdipzone.com';
$port = 80;
$errno = '';
$errstr = '';
$timeout = 30;
$url = '/socket/postapi.php';
  
$param = array(
  'name' => 'fdipzone',
  'gender' => 'man',
  'photo' => file_get_contents('photo.jpg')
);
  
$data = http_build_query($param);
  
// create connect
$fp = fsockopen($host, $port, $errno, $errstr, $timeout);
  
if(!$fp){
  return false;
}
  
// send request
$out = "POST ${url} HTTP/1.1\r\n";
$out .= "Host:${host}\r\n";
$out .= "Content-type:application/x-www-form-urlencoded\r\n";
$out .= "Content-length:".strlen($data)."\r\n";
$out .= "Connection:close\r\n\r\n";
$out .= "${data}";
  
fputs($fp, $out);
  
// get response
$response = '';
while($row=fread($fp, 4096)){
  $response .= $row;
}
  
fclose($fp);
  
$pos = strpos($response, "\r\n\r\n");
$response = substr($response, $pos+4);
  
echo $response;
?>

  postapi.php

  ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
define('UPLOAD_PATH', dirname(__FILE__).'/upload');
  
$name = $_POST['name'];
$gender = $_POST['gender'];
$photo = $_POST['photo'];
  
$filename = time().'.jpg';
file_put_contents(UPLOAD_PATH.'/'.$filename, $photo, true);
  
echo 'name='.$name.'
'
;
echo 'gender='.$gender.'
'
;
echo '.$filename.'">';
?>

  3.上传文件

  file.php

  ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
$host = 'demo.fdipzone.com';
$port = 80;
$errno = '';
$errstr = '';
$timeout = 30;
$url = '/socket/fileapi.php';
  
$form_data = array(
  'name' => 'fdipzone',
  'gender' => 'man',
);
  
$file_data = array(
  array(
    'name' => 'photo',
    'filename' => 'photo.jpg',
    'path' =>'photo.jpg'
  )
);
  
// create connect
$fp = fsockopen($host, $port, $errno, $errstr, $timeout);
  
if(!$fp){
  return false;
}
  
// send request
srand((double)microtime()*1000000);
$boundary = "---------------------------".substr(md5(rand(0,32000)),0,10);
  
$data = "--$boundary\r\n";
  
// form data
foreach($form_data as $key=>$val){
  $data .= "Content-Disposition: form-data; name=\"".$key."\"\r\n";
  $data .= "Content-type:text/plain\r\n\r\n";
  $data .= rawurlencode($val)."\r\n";
  $data .= "--$boundary\r\n";
}
  
// file data
foreach($file_data as $file){
  $data .= "Content-Disposition: form-data; name=\"".$file['name']."\"; filename=\"".$file['filename']."\"\r\n";
  $data .= "Content-Type: ".mime_content_type($file['path'])."\r\n\r\n";
  $data .= implode("",file($file['path']))."\r\n";
  $data .= "--$boundary\r\n";
}
  
$data .="--\r\n\r\n";
  
$out = "POST ${url} HTTP/1.1\r\n";
$out .= "Host:${host}\r\n";
$out .= "Content-type:multipart/form-data; boundary=$boundary\r\n"; // multipart/form-data
$out .= "Content-length:".strlen($data)."\r\n";
$out .= "Connection:close\r\n\r\n";
$out .= "${data}";
  
fputs($fp, $out);
  
// get response
$response = '';
while($row=fread($fp, 4096)){
  $response .= $row;
}
  
fclose($fp);
  
$pos = strpos($response, "\r\n\r\n");
$response = substr($response, $pos+4);
  
echo $response;
?>
首页 1 2 尾页
本文纠错】【告诉好友】【打印此文】【返回顶部
将考试网添加到收藏夹 | 每次上网自动访问考试网 | 复制本页地址,传给QQ/MSN上的好友 | 申请链接 | 意见留言 TOP
关于本站  网站声明  广告服务  联系方式  站内导航  考试论坛
Copyright © 2007-2013 中华考试网(Examw.com) All Rights Reserved