php 使用 cURL 传递 $_POST 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:14:06  来源:igfitidea点击:

Passing $_POST values with cURL

phppostcurl

提问by Scott Gottreu

How do you pass $_POSTvalues to a page using cURL?

您如何$_POST使用将值传递给页面cURL

回答by Ross

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);
curl_close($handle)

We have two options here, CURLOPT_POSTwhich turns HTTP POST on, and CURLOPT_POSTFIELDSwhich contains an array of our post data to submit. This can be used to submit data to POST<form>s.

我们在这里有两个选项,CURLOPT_POST打开 HTTP POST,并CURLOPT_POSTFIELDS包含要提交的帖子数据数组。这可用于向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.

需要注意的是curl_setopt($handle, CURLOPT_POSTFIELDS, $data);,$data 有两种格式,这决定了 post 数据的编码方式。

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

    $data = array('name' => 'Ross', 'php_master' => true);
    curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
    
  2. $dataas 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));
    
  1. $dataas an 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));
    

I hope this will help others save their time.

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

See:

看:

回答by Mark Biek

Ross has the right ideafor POSTing the usual parameter/value format to a url.

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

I recently ran into a situation where I needed to POST some XML as Content-Type "text/xml" without any parameter pairs so here's how you do that:

我最近遇到了一种情况,我需要将一些 XML 作为 Content-Type "text/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);

In my case, I needed to parse some values out of the HTTP response header so you may not necessarily need to set CURLOPT_RETURNTRANSFERor CURLOPT_HEADER.

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

回答by Sapnandu

$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);

回答by Julian

Another simple PHP example of using cURL:

另一个使用 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
?>

Example found here: http://devzone.co.in/post-data-using-curl-in-php-a-simple-example/

在这里找到的例子:http: //devzone.co.in/post-data-using-curl-in-php-a-simple-example/

Instead of using curl_setoptyou can use curl_setopt_array.

而不是使用curl_setopt您可以使用curl_setopt_array

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

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

回答by Andy Griffin

Check out this pagewhich has an example of how to do it.

查看此页面,其中包含如何执行此操作的示例。

回答by Tolo Palmer

Check out the cUrl PHP documentation page. It will help much more than just with example scripts.

查看cUrl PHP 文档页面。它将不仅仅是示例脚本的帮助。

回答by Aniket B

$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);

回答by Mohammad Faisal Islam

<?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.