0

I have an issue with a form I am trying to POST to a PHP page which then CURLs it to a Slim API endpoint but for some reason (done a lot of searching and trying different methods) I can't seem to get the form arrays to send to Slim.

header('Content-Type: application/json');
$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => "http://endpoint/add",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",

    CURLOPT_POSTFIELDS => array(
        'first_name' => $_POST['user']['first_name'],
        'last_name' => $_POST['user']['last_name'],
        'conditions[]' => '1',
        'conditions[]' => '2',
        'conditions[]' => '3'
    ),
    CURLOPT_HTTPHEADER => array(
        "Authorization: Bearer " . $_POST['token'],
        "cache-control: no-cache",
        "content-type: multipart/form-data"
    ),
));

When I test this in Postman the data submits correctly however when using it online for some reason I'm either only getting the last "conditions" value (3) or a string "Array". I see this by var_dump on the Slim API side.

As mentioned, I know Slim works as when I POST in Postman everything submits correctly.

Any suggestions?

user1020496
  • 99
  • 1
  • 9
  • Could [serialize()](https://www.php.net/manual/fr/function.serialize.php) and [unserialize()](https://www.php.net/manual/fr/function.unserialize.php) solve your problem ? – Oddrigue Feb 06 '20 at 10:10
  • If you post to an endpoint under your control, have you tried debugging? It should not be too compliated to dump the whole `$_POST`, add it to your question and explain what **exactly** should be different – Nico Haase Feb 06 '20 at 10:14
  • I do control the endpoint however my understanding is I cannot post to an endpoint that is under a different domain directly and CURL has been recommended to use. – user1020496 Feb 06 '20 at 10:18
  • Yes that does. Sorry I missed that. Have to http_build_query. Thanks everyone! – user1020496 Feb 06 '20 at 10:22
  • `"content-type: multipart/form-data"` ? I don't see any `CURLFile` though. – nice_dev Feb 06 '20 at 10:25
  • No file. That was just a post I came across that suggested using multipart as a header value. Needless to say it didn't work. – user1020496 Feb 06 '20 at 10:25

2 Answers2

1
CURLOPT_POSTFIELDS => array(
    'first_name' => $_POST['user']['first_name'],
    'last_name' => $_POST['user']['last_name'],
    'conditions[]' => '1',
    'conditions[]' => '2',
    'conditions[]' => '3'
),

That conditions[] “syntax” works for form field names, PHP will then automatically create an array out of those parameters.

It does not work in code - you have just overwritten the key conditions[] three times here, so of course only the last value survives.

You simply want something like 'conditions[]' => ['1', '2', '3'] in that place.

04FS
  • 5,660
  • 2
  • 10
  • 21
  • Thanks for responding. I did try putting the conditions in an array but got the actual word "Array" on the end point. $post_data = $request->getParsedBody(); var_dump($post_data); – user1020496 Feb 06 '20 at 10:14
  • Sorry, the `[]` still need to be included in the parameter name, so that PHP will know that it is supposed to create an array from that again. Edited. – 04FS Feb 06 '20 at 10:15
0

Thanks for that good question! I would have done the same mistake ;-)

It seems that the answer is in this other stackoverflow question: How do I use arrays in cURL POST requests

curl_setopt_array($curl, array(
    CURLOPT_URL => "http://endpoint/add",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",

    CURLOPT_POSTFIELDS => array(
        'first_name' => $_POST['user']['first_name'],
        'last_name' => $_POST['user']['last_name'],
        'conditions' => array(
            '1',
            '2',
            '3'
        )
    ),
    CURLOPT_HTTPHEADER => array(
        "Authorization: Bearer " . $_POST['token'],
        "cache-control: no-cache",
        "content-type: multipart/form-data"
    ),
));
Patrick Janser
  • 3,318
  • 1
  • 16
  • 18
  • If there already is an answer to the question, it should be closed as a duplicate instead of answered. – M. Eriksson Feb 06 '20 at 10:18
  • @MagnusEriksson Ok, thanks! I don't know how to do that. I expect it's the writer that has to do that? – Patrick Janser Feb 06 '20 at 10:20
  • 1
    When you have enough rep, you can vote to close the question as a duplicate of another question. When enough people have voted, it will be closed. Until you have enough rep, you can simply post a comment with the link instead. – M. Eriksson Feb 06 '20 at 10:21