dsjhejw3232
2013-04-10 10:02
浏览 121
已采纳

通过cURL POST发送数组

Hi i am testing a scenario for REST web service for mobile app.During testing i need to send an array to my php programme using post method. which i am doing through cURL console. rest of the thing is working fine except passing an array. Please suggest any changes.

following code i am passing in cURL console

C:\curlw32>curl -H "Content-Type: application/json" -X POST http://localhost/slim-login/api/submit -d "{\"specialtyCheckbox\":\"[1,2,3]\"}"

and here is the php code for catching it

$request = Slim::getInstance()->request();
$onsubmit_content = json_decode($request->getBody());
$spec=$onsubmit_content->specialtyCheckbox;
echo json_encode(count($spec));

Here the length of the array it is showing 1.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongshadu2546 2013-04-10 10:12
    已采纳

    dont use quotes around your array (if you want to send a json array ):

    "{\"specialtyCheckbox\":[1,2,3]}"
    
    点赞 打赏 评论
  • doubi5520 2013-04-10 10:12

    Can you try passing your array through a PHP script( using CURL)

    $ch = curl_init ($url); // your URL to send array data
    curl_setopt ($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); // Your array field
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec ($ch);
    
    print_r($result);
    
    点赞 打赏 评论
  • dprntkxh703029 2015-02-03 07:15

    If you wanna pass the array over the CURL call the answer is very simple. Put all the input parameters in array like-

    $post = array('prgmCode'=>'3',
                    'userStateCode'=>'TA',
                    );
    

    If you need to pass an input array on this you can set

    `$chkBoxArr = array(1,2,3,4);
    $post = array('prgmCode'=>'3',
                    'userStateCode'=>'TA',
                    'chkBoxArr'=>$chkBoxArr
                    );`
    

    You can use function http_build_query() to make a query string from the array.

    `$data = http_build_query($post);`
    

    And set the data to your curl

    `curl_setopt($curlSession, CURLOPT_POSTFIELDS, $data);`
    

    Check the request in the server side. Done. :) :)

    点赞 打赏 评论

相关推荐 更多相似问题