发布日期:2018-03-26
如何使用cURL函数将$ _POST值传递到页面?+ 查看更多
如何使用cURL函数将$ _POST值传递到页面?
+ 查看更多
发布日期:2018-03-26 13:50
分类:PHP
浏览次数:59
如何使用cURL函数将$ _POST值传递到页面?
回答
这样应该是可以工作的
$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);
我们有两个参数,CURLOPT_POST它打开HTTP POST,和CURLOPT_POSTFIELDS,可以让我们填写需要提交的数据。 这可以用于向POST <form>提交数据。
重要的是注意curl_setopt($ handle,CURLOPT_POSTFIELDS,$ data); 取两种格式的$ data,这决定了post数据如何编码。
1.$ data as array():数据将作为multipart / form-data发送,并不总是被服务器接受。
$data = array('name' => 'Ross', 'php_master' => true); curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
2.$ data as url encoded string:数据将作为application / x-www-form-urlencoded发送,这是提交的html表单数据的默认编码。
$data = array('name' => 'Ross', 'php_master' => true); curl_setopt($handle, CURLOPT_POSTFIELDS, http_build_query($data));
2.$ data as url encoded string:数据将作为application / x-www-form-urlencoded发送,这是提交的html表单数据的默认编码。