|
php利用fsockopen GET/POST提交表单及上传文件,具体内容如下
1.GET
get.php
'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
2.POST
post.php
'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 = "OST ${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
3.上传文件
file.php
'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 = "OST ${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;
?>
fileapi.php
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:php文件上传表单摘自drupal的代码php中关于普通表单多文件上传的处理方法美图秀秀web开放平台--PHP流式上传和表单上传示例分享PHP中上传多个文件的表单设计例子php+html5使用FormData对象提交表单及上传图片的方法php上传图片获取路径及给表单字段赋值的方法thinkphp表单上传文件并将文件路径保存到数据库中php表单文件iframe异步上传实例讲解
|
|