使用 cURL 传递 $_POST 值 [英] Passing $_POST values with cURL
问题描述
如何使用 cURL
将 $_POST
值传递给页面?
How do you pass $_POST
values to a page using cURL
?
推荐答案
应该没问题.
$data = array('name' => 'Ross', 'php_master' => true);
// You can POST a file by prefixing with an @ (for <input type="file"> fields)
$data['file'] = '@/home/user/world.jpg';
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
curl_exec($handle);
curl_close($handle)
这里有两个选项,CURLOPT_POST
开启 HTTP POST,CURLOPT_POSTFIELDS
包含我们要提交的帖子数据数组.这可用于将数据提交到 POST
s.
We have two options here, CURLOPT_POST
which turns HTTP POST on, and CURLOPT_POSTFIELDS
which contains an array of our post data to submit. This can be used to submit data to POST
<form>
s.
需要注意的是,curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
采用两种格式的 $data,这决定了帖子数据的编码方式.
It is important to note that curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
takes the $data in two formats, and that this determines how the post data will be encoded.
$data
作为array()
:数据将作为multipart/form-data
发送,这并不总是被服务器接受.
$data
as anarray()
: The data will be sent asmultipart/form-data
which is not always accepted by the server.
$data = array('name' => 'Ross', 'php_master' => true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
$data
作为 url 编码字符串:数据将作为 application/x-www-form-urlencoded
发送,这是提交的默认编码html 表单数据.
$data
as url encoded string: The data will be sent as application/x-www-form-urlencoded
, which is the default encoding for submitted html form data.
$data = array('name' => 'Ross', 'php_master' => true);
curl_setopt($handle, CURLOPT_POSTFIELDS, http_build_query($data));
我希望这能帮助其他人节省时间.
I hope this will help others save their time.
见:
这篇关于使用 cURL 传递 $_POST 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!