使用cURL传递$ _POST值 [英] Passing $_POST values with cURL
问题描述
如何使用 cURL
将 $ _ POST
值传递到页面?
应该正常工作。
$ data = array 'name'=>'Ross','php_master'=> true);
//可以通过前缀@(用于< input type =file>字段)来POST文件
$ data ['file'] ='@ home /user/world.jpg';
$ handle = curl_init($ url);
curl_setopt($ handle,CURLOPT_POST,true);
curl_setopt($ handle,CURLOPT_POSTFIELDS,$ database);
curl_exec($ handle);
我们有两个选项, CURLOPT_POST
打开HTTP POST,并且 CURLOPT_POSTFIELDS
其中包含要提交的我们的发布数据的数组。这可用于向 POST
< form>
提交数据。
$重要的是要注意
curl_setopt($ handle,CURLOPT_POSTFIELDS,$ data);
需要$的-
$ data这两种格式的数据作为
数组()
:数据将作为multipart / form-data
$ data = array('name'=>'Ross','php_master '=> true);
curl_setopt($ handle,CURLOPT_POSTFIELDS,$ data);
-
$ data
string:数据将作为application / x-www-form-urlencoded
发送,这是提交的html表单数据的默认编码。$ data = array('name'=>'Ross','php_master'=> true);
curl_setopt($ handle,CURLOPT_POSTFIELDS,http_build_query($ data));
我希望这可以帮助他人节省时间。
查看:
How do you pass $_POST
values to a page using cURL
?
Should work fine.
$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);
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.
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
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
as url encoded string: The data will be sent asapplication/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.
See:
这篇关于使用cURL传递$ _POST值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!