Search code examples
phpcurlhttp-request

CURLOPT POSTFIELDS not attatching data to HTTP Request


I've been trying to send something via HTTP request, using the CURLOPT POSTFIELDS and the data doesn't seem to transfer. Am I missing something?

        function sendTestCase($caseArgs){
        try{
            $ch = curl_init();
            $sendData = http_build_query($caseArgs);
             curl_setopt($ch, CURLOPT_POSTFIELDS, $sendData);
            curl_setopt($ch, CURLOPT_URL, 'http://localhost:8888/testrail/index.php?/miniapi/add_case/');  
            curl_setopt($ch, CURLOPT_HEADER, false);
            curl_setopt($ch, CURLOPT_POST, 1);

            curl_setopt($ch, CURLOPT_HTTPHEADER,array("Expect:"));

            curl_exec($ch);
        }
        catch (HttpException $ex){
            echo $ex."<-Exception";
 }

 curl_close($ch);
    }

Solution

  • POSTFIELDS is perfectly capable of accepting an array and curl will do the url-building for you. As it stands now, you're passing in a string to curl (which happens to contain your form values), but not passing in a field name, so curl is sending out a bare string. Try this instead:

     curl_setopt($ch, CURLOPT_POSTFIELDS, $caseArgs);