1

i am trying to pass an array to an API, the API takes array in following format when i run it in POSTMAN (raw form),

{
"records": [
    {
    "content": "50.150.50.55",
    "type": "A",
    "name": "test.mauqe.com",
    "prio": null,
    "ttl": 3600
    }
    ]
}

while am trying to pass the array in my code in this format,

 $data = array(
             "content" => "50.150.50.55",
             "type" => "A",
             "name" => "gulpanra.mauqe.com",
             "prio" => "null",
             "ttl" => "3600"
             );

i don't understand, whats the problem. response said error (Data sending format error). plz help

2
  • 2
    Please post a more complete code example, which shows how you are calling the functions that you are using to send this data from php to this API service. Commented Jan 5, 2015 at 5:31
  • are you forgetting to add records? and have you tried 3600 as a number instead of a string? and then tried null as a null instead of a string? Commented Jan 5, 2015 at 5:31

5 Answers 5

2

The API expects an array of maps. The following is an array of maps.

[
    {
    "content": "50.150.50.55",
    "type": "A",
    "name": "test.mauqe.com",
    "prio": null,
    "ttl": 3600
    },
    {},
    {},
    ...
]

What you are passing is not the same. You're passing in a single map

{
         "content" => "50.150.50.55",
         "type" => "A",
         "name" => "gulpanra.mauqe.com",
         "prio" => "null",
         "ttl" => "3600"
 }

Try amending $data to:

$data = array();

array_push($data['records'], array(
         "content" => "50.150.50.55",
         "type" => "A",
         "name" => "gulpanra.mauqe.com",
         "prio" => "null",
         "ttl" => "3600"
));
Sign up to request clarification or add additional context in comments.

4 Comments

This is most likely the correct one, but it also needs it as json ;-)
exactly ..........you are right,i have already convert into json, but how to convert my array into array of maps. pleaseeee
I assume that the json_encode is already sorted, as in the OP already had it. See the final part of the solution, where I have suggested how to declare $data.
i have already tried, but the same error...........you understand my question, but try to give me the corract solution plz
1
<?php
$data = array('records' => array());
$data['records'][] = array( 

                "content" => "50.150.50.55",
                "type" => "A",
                "name" => "gulpanra.mauqe.com",
                "prio" => null,
                "ttl" => 3600

        );

$json_output = json_encode( $data );
echo $json_output;
?>

This will give the following as output:

{"records":[{"content":"50.150.50.55","type":"A","name":"gulpanra.mauqe.com","prio":null,"ttl":3600}]}

1 Comment

you are the genius (Y)
1

Use json_encode to convert your array in json format and then pass it to the api.

The Api you are using is expecting data in json format.

$data = json_encode($data);

Comments

0

You will need to convert array into json format in order to pass it to the api. use json_encode(). Use the code below

$array = array( "content" => "50.150.50.55", "type" => "A", "name" => "gulpanra.mauqe.com", "prio" => "null", "ttl" => "3600" );
$data = json_encode($data); // Pass this to API

Hope this helps you

Comments

0

you have to make array outside for example

you are using this type of array to encode

$data['records'] = array(
       'content' => '50.150.50.55',
       and so on
);

change this array to this

$data = array(
  'content' => '50.150.50.55',
   and so on
);

this will help

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.