0

I am trying to use curl with an API and post values to it. I'm posting the parameters in an associative array where all the values are either strings, integers or booleans apart from one value which is another array. (So there is an array within another array.)

The problem is: the second array isn't being sent properly and I can't get it to work. Originally the issue was 'array to string conversion' so I then started using http_build_query() around the params array and it stopped that issue but it sill isn't working.

I know it's probably an issue with my code and not the external API as other developers are using the API fine with other languages.

PHP Code:

    $url = '{url}';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    $headers = array("X-Auth-Token: $json_token");
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $params = array(
            'name' => 'sample_name',
            'types' => array('web'),
            'limit' => 2,
            'auto' => true
    );
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
    $out = curl_exec($ch);
    curl_close($ch);

3 Answers 3

1

Neither of form submission formats support nested (multi-dimensional) arrays. In other words you cannot send nested arrays in POST request as form-encoded data (CURL uses application/x-www-form-urlencoded format by default). Most likely you've misinterpreted API specs. Perhaps the API accept data in other format, for instance, JSON which allows any level of nesting.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes! I changed the content type and encoded the array to json and it worked! Thanks!
@user5331188 You could upvote this answer as well if it solve your problem
1

Try with serialize

// Data to post 
$postData = array( 
  'name' = 'foo', 
  'data' = serialize(array(1,2,3,4)), 
  'value' = 'bar' 
); 

// Will not error 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); 
?>

Check here : https://www.php.net/manual/en/function.curl-setopt.php#99108

1 Comment

instead of serialize, I used json_encode
0

Try this...

$url = '{url}';
$params = array(
        'name' => 'sample_name',
        'types' => array('web'),
        'limit' => 2,
        'auto' => true
);
$data_string = json_encode($params);                                                                                   

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   

$out = curl_exec($ch);
curl_close($ch);

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.