php curl编码类型设置、文件上传与接收数据

来源:互联网 发布:python开发过哪些软件? 编辑:程序博客网 时间:2024/06/14 01:24

1、PHP中CURL的CURLOPT_POSTFIELDS参数使用细节
http://www.jb51.net/article/48185.htm

如果设定content-type(CURLOPT_HTTPHEADER,curl_setopt ,curl_setopt_array),按照设定;如果没有设定,curl自动添加(multipart/form-data 或 application/x-www-form-urlencoded)。

2、http-关于application/x-www-form-urlencoded等字符编码的解释说明
http://blog.csdn.net/klarclm/article/details/7711021

3、PHP 使用 curl 提交 json 格式数据
http://www.cnblogs.com/caly/archive/2013/04/11/3013980.html

4、浅谈php表单提交中enctype属性
http://blog.csdn.net/freshlover/article/details/8603648

5、PHP输入流php://input介绍
http://www.jb51.net/article/31317.htm

$data='{"content":"kg9Ho+wf8bpNTPsqRUFbGifivt81fXk2dTiVTVgotKjI2Uv1b9Vq\/HwydVQu2+LM"}';$this->_options[CURLOPT_POSTFIELDS] = $data;

curl设置了 ‘Content-Type’ => ‘text/plain’ 头,post的数据为json串,可以用file_get_contents(‘php://input’) 获取数据 ,
$_GET、$_POST为空。file_get_contents(‘php://input’) 获取的数据:

string ‘{“content”:”kg9Ho+wf8bpNTPsqRUFbGifivt81fXk2dTiVTVgotKjI2Uv1b9Vq\/HwydVQu2+LM”}’
(length=79)

curl没有设置头,post的数据为json串,(会自动添加application/x-www-form-urlencoded),可以用file_get_contents(‘php://input’) 获取数据,$_GET为空, $_POST的内容:

array (size=1)
‘{“content”:”kg9Ho_wf8bpNTPsqRUFbGifivt81fXk2dTiVTVgotKjI2Uv1b9Vq\/HwydVQu2_LM”}’ => string ” (length=0)

file_get_contents(‘php://input’) 获取的数据:

  string '{"content":"kg9Ho+wf8bpNTPsqRUFbGifivt81fXk2dTiVTVgotKjI2Uv1b9Vq\/HwydVQu2+LM"}'  (length=79)

6、curl含有上传文件

$file = realpath(ltrim($item, '@'));$item = new CURLFile($file);$item->setPostFilename($filename);$data['dataFile']=$item;//$data数组中还有其他数据$this->_options[CURLOPT_POSTFIELDS] = $data;curl_setopt_array($this->_curl, $this->_options);curl_exec($this->_curl);

new CURLFile

会自动设置content-type : multipart/form-data ; boundary=————————70c909cf0943e42f。如果同时设置了content-type : multipart/form-data头,会自动添加boundary标识。

多个文件同时上传时$data格式:
这里写图片描述

原创粉丝点击