PHP如何响应移动端(android or ios)的post请求(使用file_get_contents)
来源:互联网 发布:python turtle填色 编辑:程序博客网 时间:2024/04/25 15:07
file_get_contents这个函数是PHP在处理http请求时接受移动端数据的非常重要的一个方法
他的作用是把整个文件读入一个字符串中。
file_get_contents(path,include_path,context,start,max_length)
可选。规定文件句柄的环境。
context 是一套可以修改流的行为的选项。若使用 null,则忽略。
start可选。规定在文件中开始读取的位置。该参数是 PHP 5.1 新加的。max_length可选。规定读取的字节数。该参数是 PHP 5.1 新加的。所以对于一个移动端上传一个文件的请求,以android为例子:
private static final int BUFFER = 2048;private void testPost() {String url = "http://2.novelread.sinaapp.com/framework-sae/index.php?c=main&a=getPostBodyAndContent";DefaultHttpClient client = null;HttpRequestBase http = null;HttpResponse response = null;ByteArrayOutputStream outStream = null;byte data[] = new byte[BUFFER];try {http = new HttpPost(url);//在手机SD上有一张11.jpg的图片File f = new File(Environment.getExternalStorageDirectory(), "11.jpg");long length = f.length();//发给服务端的数据使用byte的格式outStream = new ByteArrayOutputStream();BufferedInputStream in_buf_stream = null;//先写入文件(11.jpg)的大小String s = String.valueOf(length);s = String.format("%08d", length);outStream.write(s.getBytes());//再写入11.jpgin_buf_stream = new BufferedInputStream(new FileInputStream(f), BUFFER);int count;while ((count = in_buf_stream.read(data, 0, BUFFER)) != -1) {outStream.write(data, 0, count);}ByteArrayEntity att_byte = null;if (outStream != null) {att_byte = new ByteArrayEntity(outStream.toByteArray());try {if (outStream != null) {outStream.close();outStream = null;}} catch (Throwable th) {}//用setEntity的方式写到http请求中((HttpEntityEnclosingRequestBase) http).setEntity(att_byte);}DefaultHttpClient httpClient = RestHttpClient.getClient().getHttpClient();response = httpClient.execute(http);int code = response.getStatusLine().getStatusCode();if (code == HttpStatus.SC_OK) {HttpEntity respEntity = response.getEntity();InputStream inputStream = respEntity.getContent();StringBuilder builder = new StringBuilder();InputStreamReader Inreader = new InputStreamReader(inputStream);BufferedReader reader = new BufferedReader(Inreader);String line;try {while ((line = reader.readLine()) != null) {builder.append(line).append("\n");}} finally {try {reader.close();} catch (IOException e) {}}}try {if (att_byte != null) {att_byte.consumeContent();att_byte = null;}} catch (Throwable th) {}} catch (ClientProtocolException e) {} catch (Exception e) {} }
这个post请求是先写入文件大小,然后才是文件的内容,这个时候PHP这边
$raw .= file_get_contents('php://input');
这里$raw就存储了客户端传来的entity里的以byte方式存储的数据
然后我们需要分割这些数据:
$length = substr($raw,0,8);
$content = substr($raw,8,$length);
这样我们就得到文件的内容$content,我们可以把这个内容保存成一个文件,这里是保存在sae的domain上面,'file'是我的domain名称,我在'file'上创建了一个文件夹test
<span style="white-space:pre"></span> $storage = new SaeStorage(); $domain = 'file'; $destFileName = "/test/".$img_name; $result = $storage->write($domain,$destFileName, $content, -1,$attr,false); echo $result;
0 0
- PHP如何响应移动端(android or ios)的post请求(使用file_get_contents)
- file_get_contents的post请求
- Android+PHP 使用HttpClient提交POST的请求,使用JSON解析响应
- php 使用file_get_contents的问题
- PHP file_put_contents&file_get_contents的使用
- PHP模拟发送POST请求之四、加强file_get_contents()发送POST请求
- file_get_contents模拟GET/POST请求
- Flask 使用重定向作为POST请求的响应
- 【iOS-网络】使用异步的post请求
- 【Android网络请求】如何使用Volley发送POST请求
- 同时替换<?php和<?||file_get_contents的使用
- IOS 中使用 POST请求与GET请求的区别
- 使用fiddler监听分析移动端的请求和响应方法
- 如何使用fiddler2拦截移动端app的数组请求
- 使用file_get_contents提交http post
- 使用file_get_contents提交http post
- 使用file_get_contents提交http post
- php 模拟浏览器 发送 post 和 get 请求(兼容curl + file_get_contents)
- 几种获取本机IPv6地址的方法
- abstract class和interface的区别详解
- 脸上长了黄褐斑怎么办
- Java Servlet调用Matlab算法
- BootCamp助理中没有“创建Windows7的USB安装盘”
- PHP如何响应移动端(android or ios)的post请求(使用file_get_contents)
- 奇异值分解(SVD)的之低秩近似和特征降维
- Java项目导出为可执行jar包,并在windows/linux下运行
- Win7机器上安装Ubuntu 14.0.4
- struts2 获取session的方法,sessionAware 接口的使用
- javaniu用户头像如何实现的啊,能否提供一份代码呢
- Ruby中each,map,collect,inject对比
- Linux内核--网络协议栈深入分析(三)--BSD socket和传输层sock
- Android版本百度地图开发(一)——创建应用