MSSql MySql Python javascript PHP java DotNet Ruby Hackme Takeawy Code Twitter Facebook

The house of developers, A website to classify and rank developers

 
 
 
 
Country Rank: 21
World Rank: 146
Profile Viewed: 2015
Points: 3975
25 Mar 2011

PHP: How to make a post request using CURL

function do_post_request($url, $params = array(), $head = array()) {
    //
    // request body
    //
    $body = array();
    foreach($params as $param => $value) {
        $body[] = urlencode($param).'='.urlencode($value);
    }
    $body = implode('&', $body);

    //
    // request headers
    //
    $head[] = "Content-Type: application/x-www-form-urlencoded; charset=UTF-8";
    $head[] = "Content-Length: ".strlen($body);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $head);

    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}
You can use this function to make a post request using php curl extension, you can also send your customized HTTP headers and the post parameters.
Category: PHP
Facebook Share Delicious Share Digg Share Google Buzz Share My Space Share Reddit Share Stumbleupon Share Technorati Share Twitter Share