duanli6834
2012-11-23 11:30
浏览 30
已采纳

如何使用PHP在REST中将数组作为post请求发送?

Here is my code,

$ip = array([0]=>'1.1.1.1' [1]=> '2.2.2.2')

$ux = RestClient::post($url,array('requestType'=>'Ip', 
                                                     'username' => 'user', 
                                                             'pass' =>'user',
                        'type'=>$type,
                        'ip'=>array($ip)                            
                            ));
  echo $ux->getResponse();

How to post 'ip' at server side? When I use $_POST['id'], it returns 'Array' as string.

图片转代码服务由CSDN问答提供 功能建议

这是我的代码,

  $ ip = array(  [0] =>'1.1.1.1'[1] =>'2.2.2.2')
 
 $ ux = RestClient :: post($ url,array('requestType'=>'Ip',  \''username'=>'user',
'pass'=>'user',
'type'=> $ type,
'ip'=> array($ ip)
  )); 
 echo $ ux-> getResponse(); 
   
 
 

如何在服务器端发布'ip'? 当我使用$ _POST ['id']时,它将'Array'作为字符串返回。

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

2条回答 默认 最新

  • dongqiang1894 2012-11-23 11:34
    已采纳

    You can't post an array. You have to serialize it to a string. That could be the standard form encoding method:

    ip=1.1.1.1&ip=2.2.2.2
    

    That could be JSON:

    { "ip" : [ "1.1.1.1", "2.2.2.2" ] }
    

    That could be some XML format:

    <ips>
        <ip>1.1.1.1</ip>
        <ip>2.2.2.2</ip>
    </ips>
    

    That could be something else.

    … but what you need to do depends on what the API you are submitting to expects.

    点赞 评论
  • douyang5943 2012-11-23 11:32

    I suspect you need to use JSON for your array. Convert it with json_encode( $array )

    点赞 评论

相关推荐 更多相似问题