phphttpcurlhttp-post

PHP, cURL, and HTTP POST example?


Can anyone show me how to do a PHP cURL with an HTTP POST?

I want to send data like this:

username=user1, password=passuser1, gender=1

To www.example.com

I expect the cURL to return a response like result=OK. Are there any examples?


Solution

  • A very simple PHP example that sends an HTTP POST request to a remote site

    $url = "http://www.example.com/tester.phtml";
    $post_data = array('postvar1' => 'value1');
    
    
    $ch = curl_init($url);
    // return the response instead of sending it to stdout:
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // set the POST data, corresponding method and headers:
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
    // send the request and get the response
    $server_output = curl_exec($ch);