2012-05-14 52 views
0

我已經被告知,通過將數組傳遞到CURLOPT_POSTFIELDS將自動爲您編寫URL編碼,但出於某種原因,它不適合我。我試圖自己編碼一個字符串,但是不會在頭文件中。當我傳入數組時,它不會被編碼。cURL似乎不是在編碼數組在PHP中

這裏是我的代碼:

$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); 

編輯-----

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

它應該沒問題 - 您的陣列是什麼?我們可以看到樣品嗎?它需要是關鍵/值,就像常規表單的post字段是name/value一樣:$ caseArgs ['fname'] ='john'; $ caseArgs [ 'L-NAME'] = '史密斯'; – RiquezJP

+0

cURL不會編碼多維數組。你能向我們展示你想發送的數據嗎? – radmen

+0

@RiquezJP - 我已經附加了我正在使用的數組。感謝您的幫助和反饋! – BlackHatSamurai

回答

1

http_build_query會做URL編碼一個 多維 陣列上。

編輯:對不起。有人提到了上面的多維數組,我只是把它放在我的腦海中。

雖然有一個錯誤,但在$case[] = array(); 此行正在$case陣列的第一個元素中添加一個新數組。只需將其更改爲:$case = array();

+0

我沒有多維陣列 – BlackHatSamurai

+0

對不起,我的錯。我編輯了我的答案。 –