欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php利用fsockopen GET/POST提交表單及上傳文件

 更新時(shí)間:2017年05月22日 16:56:19   作者:左正  
這篇文章主要為大家詳細(xì)介紹了php利用fsockopen GET/POST提交表單及上傳文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

php利用fsockopen GET/POST提交表單及上傳文件,具體內(nèi)容如下

1.GET

get.php

<?php 
$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

<?php 
$name = $_GET['name']; 
$gender = $_GET['gender']; 
 
echo 'name='.$name.'<br>'; 
echo 'gender='.$gender; 
?> 

2.POST

post.php

<?php 
$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

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

3.上傳文件

file.php

<?php 
$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; 
?> 

fileapi.php

<?php 
define('UPLOAD_PATH', dirname(__FILE__).'/upload'); 
 
$name = $_POST['name']; 
$gender = $_POST['gender']; 
 
$filename = time().'.jpg'; 
 
echo 'name='.$name.'<br>'; 
echo 'gender='.$gender.'<br>'; 
if(move_uploaded_file($_FILES['photo']['tmp_name'], UPLOAD_PATH.'/'.$filename)){ 
  echo '<img src="upload/'.$filename.'">'; 
} 
?> 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • PHP設(shè)計(jì)模式(觀察者模式)

    PHP設(shè)計(jì)模式(觀察者模式)

    這篇文章主要介紹了PHP設(shè)計(jì)模式(觀察者模式),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • php使用多個(gè)進(jìn)程同時(shí)控制文件讀寫示例

    php使用多個(gè)進(jìn)程同時(shí)控制文件讀寫示例

    這篇文章主要介紹了php使用多個(gè)進(jìn)程同時(shí)控制文件讀寫示例,需要的朋友可以參考下
    2014-02-02
  • 基于Swoole實(shí)現(xiàn)PHP與websocket聊天室

    基于Swoole實(shí)現(xiàn)PHP與websocket聊天室

    本文利用Swoole來實(shí)現(xiàn)PHP+websocket的聊天室,過程介紹的很詳細(xì),對(duì)聊天室的開發(fā)很有幫助,有需要的可以參考學(xué)習(xí)。
    2016-08-08
  • laravel 5 實(shí)現(xiàn)模板主題功能(續(xù))

    laravel 5 實(shí)現(xiàn)模板主題功能(續(xù))

    前面一篇文章,我們簡(jiǎn)單討論了laravel模板主題功能,本文我們繼續(xù)探討laravel模板主題功能的實(shí)現(xiàn),本次實(shí)現(xiàn)比較重,有興趣慢慢看吧。
    2015-03-03
  • php讀取excel文件的簡(jiǎn)單實(shí)例

    php讀取excel文件的簡(jiǎn)單實(shí)例

    這篇文章介紹了php讀取excel文件的簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下
    2013-08-08
  • PHP的curl函數(shù)的用法總結(jié)

    PHP的curl函數(shù)的用法總結(jié)

    在本篇文章中小編給大家分享了關(guān)于PHP的curl函數(shù)的用法總結(jié)以及相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的讀者們學(xué)習(xí)下。
    2019-02-02
  • 關(guān)于ThinkPHP中的異常處理詳解

    關(guān)于ThinkPHP中的異常處理詳解

    和PHP默認(rèn)的異常處理不同,ThinkPHP拋出的不是單純的錯(cuò)誤信息,而是一個(gè)人性化的錯(cuò)誤頁(yè)面,下面這篇文章主要給大家介紹了關(guān)于ThinkPHP中異常處理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2018-05-05
  • TP5.0框架實(shí)現(xiàn)無限極回復(fù)功能的方法分析

    TP5.0框架實(shí)現(xiàn)無限極回復(fù)功能的方法分析

    這篇文章主要介紹了TP5.0框架實(shí)現(xiàn)無限極回復(fù)功能的方法,結(jié)合實(shí)例形式分析了thinkPHP5.0框架下無限極回復(fù)功能相關(guān)的數(shù)據(jù)庫(kù)、評(píng)論功能及界面布局實(shí)現(xiàn)方法,需要的朋友可以參考下
    2019-05-05
  • php學(xué)習(xí)Eloquent修改器源碼示例解析

    php學(xué)習(xí)Eloquent修改器源碼示例解析

    這篇文章主要為大家介紹了php學(xué)習(xí)Eloquent修改器源碼示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • PHP SPL標(biāo)準(zhǔn)庫(kù)之文件操作(SplFileInfo和SplFileObject)實(shí)例

    PHP SPL標(biāo)準(zhǔn)庫(kù)之文件操作(SplFileInfo和SplFileObject)實(shí)例

    這篇文章主要介紹了PHP SPL標(biāo)準(zhǔn)庫(kù)之文件操作(SplFileInfo和SplFileObject)實(shí)例,本文講解SplFileInfo用來獲取文件詳細(xì)信息、SplFileObject遍歷、查找指定行、寫入csv文件等內(nèi)容,需要的朋友可以參考下
    2015-05-05

最新評(píng)論