使用cURL传递$ _POST值 [英] Passing $_POST values with cURL

查看:117
本文介绍了使用cURL传递$ _POST值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 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); 需要$的


  1. $ data这两种格式的数据作为数组():数据将作为 multipart / form-data

      $ data = array('name'=>'Ross','php_master '=> true); 
    curl_setopt($ handle,CURLOPT_POSTFIELDS,$ data);


  2. $ 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.

  1. $data as an array(): The data will be sent as multipart/form-data which is not always accepted by the server.

    $data = array('name' => 'Ross', 'php_master' => true);
    curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
    

  2. $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.

See:

这篇关于使用cURL传递$ _POST值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆