客户端发送数据流到服务端PHP处理相关的
2013-5-23 9:43 Thursday  

分类: PHP 标签: http data entity 数据流 fputs 评论(81) 浏览(39766)

APP或者Unity3D在做图片上传的时候,一般都是数据流格式,此时数据流传送的时候,enctype是application/x-www-form-urlencoded,PHP可以通过file_get_contents("php://input")获取(除了在enctype="multipart/form-data"情况下是无法接收到流,其他情况下都可以),额,程序代码如下

if ($_POST) {
            $data = file_get_contents("php://input");
            parse_str($data,$d);
            file_put_contents("hh.gif", $d['data']);//写到hh.gif中,看看是否正常,不正常表示数据流被破坏
            echo 'username:'.$d['username'].'<br />';
            echo 'password:'.$d['password'].'<br />';
            exit;
        }
        function encode($params) {
            $i = 0;
            foreach ($params as $k => $v) {
                $i++;
                $str.= rawurlencode($k) . "=" . rawurlencode($v);
                if ($i < count($params))
                    $str.= "&";
            }
            return$str;
        }
        
        $data = file_get_contents('http://www.baidu.com/img/bdlogo.gif');
        $infoary= array("username"=>"aboc","password"=>"aboc",'data'=>$data);
        $encodestr= encode($infoary);
        $http_entity_body = $encodestr;
        $http_entity_type = 'application/x-www-form-urlencoded';
        $http_entity_length = strlen($http_entity_body);
        $host = $_SERVER['HTTP_HOST'];
        $port = 80;
        $path = '/3d/?act=add';
        $fp = fsockopen($host, $port, $error_no, $error_desc, 30);
        if ($fp) {
            fputs($fp, "POST {$path} HTTP/1.1\r\n");
            fputs($fp, "Host: {$host}\r\n");
            fputs($fp, "Content-Type: {$http_entity_type}\r\n");
            fputs($fp, "Content-Length: {$http_entity_length}\r\n");
            fputs($fp, "Connection: close\r\n\r\n");
            fputs($fp, $http_entity_body . "\r\n\r\n");

            while (!feof($fp)) {
                $d .= fgets($fp, 4096);
            }
            fclose($fp);
            echo $d;
        }

 

 

 

rawurlencode
+1 7

留下你的看法: