php 利用 fsockopen GET/POST 提交表單及上傳文件
1.GET
get.php
01.<?php
02.$host = 'demo.fdipzone.com';
03.$port = 80;
04.$errno = '';
05.$errstr = '';
06.$timeout = 30;
07.$url = '/socket/getapi.php';
08.
09.$param = array(
10. 'name' => 'fdipzone',
11. 'gender' => 'man'
12.);
13.
14.$url = $url.'?'.http_build_query($param);
15.
16.// create connect
17.$fp = fsockopen($host, $port, $errno, $errstr, $timeout);
18.
19.if(!$fp){
20. return false;
21.}
22.
23.// send request
24.$out = "GET ${url} HTTP/1.1\r\n";
25.$out .= "Host: ${host}\r\n";
26.$out .= "Connection:close\r\n\r\n";
27.
28.fputs($fp, $out);
29.
30.// get response
31.$response = '';
32.while($row=fread($fp, 4096)){
33. $response .= $row;
34.}
35.
36.fclose($fp);
37.
38.$pos = strpos($response, "\r\n\r\n");
39.$response = substr($response, $pos+4);
40.
41.echo $response;
42.?>
getapi.php
<?php $name = $_GET['name']; $gender = $_GET['gender']; echo 'name='.$name.'<br>'; echo 'gender='.$gender; ?>