简体   繁体   English

cURL似乎不在php中编码数组

[英]cURL doesn't seem to be encoding array in php

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. 有人告诉我,通过将数组传递给CURLOPT_POSTFIELDS会自动为您执行URL编码,但是由于某种原因,它对我而言并不是这样做。 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;

} }

http_build_query will do URL encoding on a multidimensional array. http_build_query将对 多维 数组进行URL编码。

Edit: Sorry. 编辑:对不起。 Someone mentioned a multidimensional array above and I just got it in my head. 有人在上面提到了一个多维数组,而我只是想到了。

You have a mistake though, in $case[] = array(); 但是,在$case[] = array();有一个错误$case[] = array(); This line is putting a new array in the first element of the $case array. 这行代码将新数组放入$case数组的第一个元素中。 Just change it to: $case = array(); 只需将其更改为: $case = array();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM