PHP curl 上传文件
来源:互联网 发布:小米6怎么样知乎 编辑:程序博客网 时间:2024/04/26 08:32
文件上传
上传文件和前面的POST十分相似。因为所有的文件上传表单都是通过POST方法提交的。
首先新建一个接收文件的页面,命名为 upload_output.php:
print_r($_FILES);
以下是真正执行文件上传任务的脚本:
以下为引用的内容:
$url = "http://localhost/upload_output.php";
$post_data = array (
"foo" => "bar",
// 要上传的本地文件地址
"upload" => "@C:/wamp/www/test.zip" // linux 平台 这用方式 @/absolute/path/to/image.png
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
如果你需要上传一个文件,只需要把文件路径像一个post变量一样传过去,不过记得在前面加上@符号。执行这段脚本应该会得到如下输出:
用POST方法发送数据
当发起GET请求时,数据可以通过“查询字串”(query string)传递给一个URL。例如,在google中搜索时,搜索关键即为URL的查询字串的一部分:
http://www.google.com/search?q=nettuts
这种情况下你可能并不需要cURL来模拟。把这个URL丢给“file_get_contents()”就能得到相同结果。
不过有一些HTML表单是用POST方法提交的。这种表单提交时,数据是通过 HTTP请求体(request body) 发送,而不是查询字串。例如,当使用CodeIgniter论坛的表单,无论你输入什么关键字,总是被POST到如下页面:
http://codeigniter.com/forums/do_search/
你可以用PHP脚本来模拟这种URL请求。首先,新建一个可以接受并显示POST数据的文件,我们给它命名为post_output.php:
print_r($_POST);
接下来,写一段PHP脚本来执行cURL请求:
以下为引用的内容:
$url = "http://localhost/post_output.php";
$post_data = array (
"foo" => "bar",
"query" => "Nettuts",
"action" => "Submit"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 我们在POST数据哦!
curl_setopt($ch, CURLOPT_POST, 1);
// 把post的变量加上
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
执行代码后应该会得到以下结果:
这段脚本发送一个POST请求给 post_output.php ,这个页面 $_POST 变量并返回,我们利用cURL捕捉了这个输出。
- php curl 上传文件
- php curl上传文件
- PHP curl 上传文件
- PHP Curl 上传文件
- php使用curl上传文件
- php curl fsockopen 上传文件
- php使用curl上传文件
- PHP使用cURL上传文件
- PHP通过CURL上传文件
- php 通过curl上传文件
- php中curl上传文件
- php curl 实现文件上传
- php curl 模拟上传文件
- PHP 模拟curl上传文件
- 在 php 中通过 CURL 上传文件
- 在 php 中通过 CURL 上传文件
- 在 php 中通过 CURL 上传文件
- php+curl上传文件到远程服务器
- 【01背包】Uvalive4015 Caves && Uvalive3637 The Bookcase
- 作业2.29
- 浏览器缓存机制
- iOS新人学习AFNetworking(三)
- 从fragment中获取需要的控件
- PHP curl 上传文件
- java中简单的字符压缩实现,aaabbcccd压缩成3a2b3c1d
- deferred对象
- 拆半查找的递归和非递归算法
- 【leetcode】Populating Next Right Pointers in Each Node
- P51 ,12
- iOS新人学习AFNetworking(四)---AFNetworiking与ASIHttpRequest对比
- 作业2.30
- 如何使用UIAutomation进行iOS 自动化测试