通过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.

dongposhi8677
dongposhi8677 请看这里:forums.phpfreaks.com/topic/...
7 年多之前 回复

3个回答



不要在数组周围使用引号(如果要发送json数组):</ p>

  “{\”specialtyCheckbox \“:[1,2,3]}”
</ code> </ pre>
</ div>

展开原文

原文

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

"{\"specialtyCheckbox\":[1,2,3]}"

duanliyi5997
duanliyi5997 谢谢mpm :)它工作了!! 实际上我使用引号将其作为字符串传递,这就是为什么它显示其长度为一。
7 年多之前 回复
dousou2897
dousou2897 是的,但我不确定他的期望,因为他说curl适用于cli。
7 年多之前 回复
douyi1855
douyi1855 它实际上并不是你不需要的,但你不能在那里拥有它们;-)“[ - ]”只不过是包含这些字符的文本文字。
7 年多之前 回复



您可以尝试通过PHP脚本传递数组(使用CURL)</ p>

   $ ch = curl_init($ url);  //发送数组数据的URL 
curl_setopt($ ch,CURLOPT_POST,true);
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ postData); //您的数组字段
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
$ result = curl_exec($ ch);

print_r($ result);
</ code> </ pre>
</ DIV>

展开原文

原文

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

donglianjiang9321
donglianjiang9321 酷......它与mpm的回应有关
7 年多之前 回复
dongyuelian9602
dongyuelian9602 Sandy我不需要通过脚本运行它,因为我只是测试它的web服务,最终将被移动应用程序team.console使用对我来说没那么“努力”!
7 年多之前 回复

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. :) :)

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐