Passing $_POST values with cURL

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

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

8条回答
  •  情话喂你
    2020-11-22 16:59

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

提交回复
热议问题