通过cURL传递$ _POST值


Answers:


168

应该工作正常。

$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 <form>


重要的是要注意,curl_setopt($handle, CURLOPT_POSTFIELDS, $data);$ data采用两种格式,并且这确定了将如何对邮政数据进行编码。

  1. $dataas array():数据将以multipart/form-data服务器并不总是接受的方式发送。

    $data = array('name' => 'Ross', 'php_master' => true);
    curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
  2. $data作为url编码的字符串:数据将以形式发送application/x-www-form-urlencoded,这是提交的html表单数据的默认编码。

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

我希望这可以帮助其他人节省时间。

看到:


您的笔记至少节省了我一个小时的调试时间。谢谢。
Vivek Kumar

30

Ross具有将常用参数/值格式发布到url 的正确想法

最近,我遇到了一种情况,我需要以内容类型“ text / xml”的形式发布一些XML,而没有任何参数对,因此您可以按照以下方式进行操作:

$xml = '<?xml version="1.0"?><stuff><child>foo</child><child>bar</child></stuff>';
$httpRequest = curl_init();

curl_setopt($httpRequest, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($httpRequest, CURLOPT_HTTPHEADER, array("Content-Type:  text/xml"));
curl_setopt($httpRequest, CURLOPT_POST, 1);
curl_setopt($httpRequest, CURLOPT_HEADER, 1);

curl_setopt($httpRequest, CURLOPT_URL, $url);
curl_setopt($httpRequest, CURLOPT_POSTFIELDS, $xml);

$returnHeader = curl_exec($httpRequest);
curl_close($httpRequest);

就我而言,我需要从HTTP响应标头中解析一些值,因此您不一定需要设置CURLOPT_RETURNTRANSFERCURLOPT_HEADER


1
这不是发帖人要的,但是恰好正是我要的东西,谢谢!
davr

我很高兴其他人发现它有帮助。
Mark Biek

1
您的“ curl_setopt($ httpRequest,CURLOPT_HTTPHEADER,array(“ Content-Type:text / xml”));“ 解决了已经花了我几个小时的事情!非常感谢:)
Alexei Tenitski

嗨,马克,如果您有时间,请您帮我一下。点击这里
jayAnn 2011年

花了我们的努力来弄清楚为什么以urlencoded发送时我的xml数据不被接受。Content-Type并没有urlencode救了我。谢谢。
塞缪尔

3
$query_string = "";

if ($_POST) {
    $kv = array();
    foreach ($_POST as $key => $value) {
        $kv[] = stripslashes($key) . "=" . stripslashes($value);
    }
    $query_string = join("&", $kv);
}

if (!function_exists('curl_init')){
    die('Sorry cURL is not installed!');
}

$url = 'https://www.abcd.com/servlet/';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($kv));
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);

curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

$result = curl_exec($ch);

curl_close($ch);

3

使用cURL的另一个简单的PHP示例:

<?php
    $ch = curl_init();                    // Initiate cURL
    $url = "http://www.somesite.com/curl_example.php"; // Where you want to post data
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POST, true);  // Tell cURL you want to post something
    curl_setopt($ch, CURLOPT_POSTFIELDS, "var1=value1&var2=value2&var_n=value_n"); // Define what you want to post
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the output in string format
    $output = curl_exec ($ch); // Execute

    curl_close ($ch); // Close cURL handle

    var_dump($output); // Show output
?>

此处找到示例:http : //devzone.co.in/post-data-using-curl-in-php-a-simple-example/

curl_setopt可以使用代替使用curl_setopt_array

http://php.net/manual/zh/function.curl-setopt-array.php


谢谢!!-您的代码curl_setopt($ch, CURLOPT_POSTFIELDS, "var1=value1&var2=value2&var_n=value_n"); // Define what you want to post为我提供了我想要的东西:)
asugrue17年



1
$url='Your url'; // Specify your url
$data= array('parameterkey1'=>value,'parameterkey2'=>value); // Add parameters in key value
$ch = curl_init(); // Initialize cURL
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);

1
您能否扩展这个答案?几行代码没有答案。
Rich Benner

1)指定您的网址2)创建参数数组3)初始化curl 4)设置curl的必需选项5)执行Curl 6)关闭Curl
Aniket B

0
<?php
    function executeCurl($arrOptions) {

        $mixCH = curl_init();

        foreach ($arrOptions as $strCurlOpt => $mixCurlOptValue) {
            curl_setopt($mixCH, $strCurlOpt, $mixCurlOptValue);
        }

        $mixResponse = curl_exec($mixCH);
        curl_close($mixCH);
        return $mixResponse;
    }

    // If any HTTP authentication is needed.
    $username = 'http-auth-username';
    $password = 'http-auth-password';

    $requestType = 'POST'; // This can be PUT or POST

    // This is a sample array. You can use $arrPostData = $_POST
    $arrPostData = array(
        'key1'  => 'value-1-for-k1y-1',
        'key2'  => 'value-2-for-key-2',
        'key3'  => array(
                'key31'   => 'value-for-key-3-1',
                'key32'   => array(
                    'key321' => 'value-for-key321'
                )
        ),
        'key4'  => array(
            'key'   => 'value'
        )
    );

    // You can set your post data
    $postData = http_build_query($arrPostData); // Raw PHP array

    $postData = json_encode($arrPostData); // Only USE this when request JSON data.

    $mixResponse = executeCurl(array(
        CURLOPT_URL => 'http://whatever-your-request-url.com/xyz/yii',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPGET => true,
        CURLOPT_VERBOSE => true,
        CURLOPT_AUTOREFERER => true,
        CURLOPT_CUSTOMREQUEST => $requestType,
        CURLOPT_POSTFIELDS  => $postData,
        CURLOPT_HTTPHEADER  => array(
            "X-HTTP-Method-Override: " . $requestType,
            'Content-Type: application/json', // Only USE this when requesting JSON data
        ),

        // If HTTP authentication is required, use the below lines.
        CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
        CURLOPT_USERPWD  => $username. ':' . $password
    ));

    // $mixResponse contains your server response.
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.