如何通過php 利用fsockopen GET/POST 提交表單及上傳檔案

來源:互聯網
上載者:User
php 利用 fsockopen GET/POST 提交表單及上傳檔案

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

<?phpdefine('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 requestsrand((double)microtime()*1000000);$boundary = "---------------------------".substr(md5(rand(0,32000)),0,10);$data = "--$boundary\r\n";// form dataforeach($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 dataforeach($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

<?phpdefine('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.'">';}?>

本篇文章講解了如何通過php 利用fsockopen GET/POST 提交表單及上傳檔案 ,更多相關內容請關注php中文網。

先關推薦:

關於mysql 最佳化 insert 效能 的相關介紹

如何使用php 常用自訂方法

如何通過php 使用異或(XOR)加密/解密檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.