Passing $_POST values with cURL

后端 未结 8 1586
野的像风
野的像风 2020-11-22 16:21

How do you pass $_POST values to a page using cURL?

相关标签:
8条回答
  • 2020-11-22 16:48

    Another simple PHP example of using cURL:

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

    Instead of using curl_setopt you can use curl_setopt_array.

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

    0 讨论(0)
  • 2020-11-22 16:51
    $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);
    
    0 讨论(0)
  • 2020-11-22 16:55

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

    0 讨论(0)
  • 2020-11-22 16:56
    $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);
    
    0 讨论(0)
  • 2020-11-22 16:57

    Ross has the right idea for POSTing the usual parameter/value format to a 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 = '<?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_RETURNTRANSFER or CURLOPT_HEADER.

    0 讨论(0)
  • 2020-11-22 16:59
    <?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.
    
    0 讨论(0)
提交回复
热议问题