我被告知通过将一个数组传递给CURLOPT_POSTFIELDS会自动为你做URL编码,但由于某种原因它并没有为我做。我曾尝试自己编码一个字符串,但这不会占用标题。当我传入一个数组时,它不会被编码。
这是我的代码:
$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;
}
答案 0 :(得分:1)
http_build_query将对多维数组进行URL编码。
编辑:抱歉。有人在上面提到了一个多维数组,我只是想到了它。
你有一个错误,$case[] = array();
这一行是在$case
数组的第一个元素中放入一个新数组。只需将其更改为:$case = array();