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

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

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

源码下载地址:点击下载

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值