php利用fsockopen GET/POST提交表单及上传文件

前端之家收集整理的这篇文章主要介绍了php利用fsockopen GET/POST提交表单及上传文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

1.GET

get.PHP

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

PHP;"> PHP $name = $_GET['name']; $gender = $_GET['gender'];

echo 'name='.$name.'
';
echo 'gender='.$gender;
?>

2.POST

post.PHP

PHP;"> $param = array(
'name' => 'fdipzone','gender' => 'man','photo' => file_get_contents('photo.jpg')
);

$data = http_build_query($param);

// create connect
$fp = fsockopen($host,$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,$pos+4);

echo $response;
?>

postapi.PHP

PHP;"> PHP 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 '<img src="upload/'.$filename.'">';
?>

3.上传文件

file.PHP

PHP;"> $form_data = array(
'name' => 'fdipzone',);

$file_data = array(
array(
'name' => 'photo','filename' => 'photo.jpg','path' =>'photo.jpg'
)
);

// create connect
$fp = fsockopen($host,$timeout);

if(!$fp){
return false;
}

// send request
srand((double)microtime()*1000000);
$boundary = "---------------------------".substr(md5(rand(0,32000)),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,$pos+4);

echo $response;
?>

fileapi.PHP

PHP;"> PHP define('UPLOAD_PATH',dirname(__FILE__).'/upload');

$name = $_POST['name'];
$gender = $_POST['gender'];

$filename = time().'.jpg';

echo 'name='.$name.'
';
echo 'gender='.$gender.'
';
if(move_uploaded_file($_FILES['photo']['tmp_name'],UPLOAD_PATH.'/'.$filename)){
echo '<img src="upload/'.$filename.'">';
}
?>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

原文链接:https://www.f2er.com/php/17419.html

猜你在找的PHP相关文章