Developer
Developer

Reputation: 1040

how to send an array as post request in REST using PHP?

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.

Upvotes: 1

Views: 14728

Answers (2)

Quentin
Quentin

Reputation: 943564

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.

Upvotes: 7

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

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

Upvotes: 0

Related Questions