代码之家  ›  专栏  ›  技术社区  ›  Scott Gottreu

通过curl传递$\u post值

  •  90
  • Scott Gottreu  · 技术社区  · 16 年前

    你如何通过 $_POST 值到页面使用 cURL ?

    8 回复  |  直到 16 年前
        1
  •  163
  •   DaAmidza    7 年前

    应该工作得很好。

    $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数据数组。这可用于将数据提交到 POST <form> S.


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

    1. $data 作为一个 array() :数据将作为 multipart/form-data 服务器并不总是接受它。

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

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

    我希望这能帮助别人节省时间。

    见:

        2
  •  29
  •   Community CDub    7 年前

    Ross has the right idea 用于将常规参数/值格式发布到URL。

    我最近遇到了一种情况,我需要将一些XML作为内容类型“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);
    

    在我的例子中,我需要从HTTP响应头中解析一些值,这样您就不必设置 CURLOPT_RETURNTRANSFER CURLOPT_HEADER .

        3
  •  3
  •   Peter Mortensen icecrime    10 年前
    $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);
    
        4
  •  3
  •   Julian    9 年前

    另一个使用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/en/function.curl-setopt-array.php

        5
  •  2
  •   Ken Liu    16 年前

    退房 this page 它有一个如何做的例子。

        6
  •  1
  •   Peter Mortensen icecrime    10 年前

    退房 cUrl PHP documentation page . 它不仅对示例脚本有帮助。

        7
  •  1
  •   Saqib Omer Dasoga    8 年前
    $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);
    
        8
  •  0
  •   Peter Mortensen icecrime    10 年前
    <?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.