0

I've been told that by passing an array into CURLOPT_POSTFIELDS will automatically do the URL encoding for you, but for some reason it isn't doing it for me. I have tried to encode a string myself, but that won't take in the header. When I pass an array in, it isn't encoded.

Here is my code:

   $ch = curl_init();

            curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json', 'Content-Type: application/x-www-form-urlencoded"));
            curl_setopt($ch, CURLOPT_URL, "http://localhost:8888/testrail/index.php?/miniapi/add_case/s2");  
            curl_setopt($ch, CURLOPT_POSTFIELDS, $caseArgs);//$caseArgs is an array from another function
            curl_setopt($ch, CURLOPT_POST, true);

            curl_setopt($ch, CURLOPT_HEADER, 1);




            curl_exec($ch);

EDIT-----

Here is the array that I am working with:

    /*Function to set the data for each individual test case*/
function setTestCase($cellValue){
            $case[]=array();
        $case['title'] = $cellValue[0];
        echo $case['title']. "<- Title"."<br/>";
        $case['type'] = $cellValue[1];
        echo $case['type']. "<- Type"."<br/>";
        $case['priority'] = $cellValue[2];
        echo $case['priority']. "<- Priority"."<br/>";


        /*$case['estimate'] = $cellValue[3];
        echo $case['estimate']. "<- Estimate"."<br/>";
        $case['milestone'] = $cellValue[4];
        echo $case['milestone']. "<- MileStone"."<br/>";
        $case['refs'] = $cellValue[5];
        echo $case['refs']. "<- Custom Refs"."<br/>";
        $case['precon'] = $cellValue[6];
        echo $case['precon']. "<- Custom Precondition"."<br/>";
        $case['steps'] = $cellValue[7];
        echo $case['steps']. "<- Custom Steps"."<br/>";
        $case['expectedresults'] = $cellValue[8];
        echo  $case['expectedresults']. "<- Expected Results"."<br/>";
        $case['testSuite'] = $cellValue[9];
        echo $case['testSuite']. "<- TestSuite"."<br/>";*/


        $caseData=array(

            'Title'=> $case['title'],
            'Type'=> $case['type'],
            'Priority'=> $case['priority'],
            'key'=> "246810",


        );

        return $caseData;

}

3
  • It should be ok - what is your array? Can we see a sample? It needs to be key/value in the same way a regular form post fields would be name/value: $caseArgs['fname']='john'; $caseArgs['lname']='smith'; Commented May 14, 2012 at 5:24
  • cURL won't encode multidimensional arrays. Can You show us the data You're trying to send? Commented May 14, 2012 at 5:55
  • @RiquezJP - I've attached the array that I am using. Thank you for your help and feed back! Commented May 14, 2012 at 15:28

1 Answer 1

1

http_build_query will do URL encoding on a multidimensional array.

Edit: Sorry. Someone mentioned a multidimensional array above and I just got it in my head.

You have a mistake though, in $case[] = array(); This line is putting a new array in the first element of the $case array. Just change it to: $case = array();

Sign up to request clarification or add additional context in comments.

1 Comment

I don't have a multidimensional array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.