Dirty Bird Design
Dirty Bird Design

Reputation: 5533

How to post a variable with a number for name

I know this is bad form, but we can't change the hidden input name as it is set by SalesForce. I have a form with an input like this:

<input type="hidden" name="00N5000000XXXXX" value="Demo_Account" />

and my PHP to post to them via cURL

$00N5000000XXXXX = $_POST['00N5000000XXXXX'];

which obviously won't work as it has number for a variable name.

When I change the name to:

$Foo = $_POST['00N5000000XXXXX'];

the back end doesn't work because it is expecting the form to submit a value with a name of 00N5000000XXXXX, not Foo or whatever I want to call it.

Obviously, Im not a PHP developer but need some advice on how to get around this. Thank you.

Upvotes: 0

Views: 73

Answers (1)

h2ooooooo
h2ooooooo

Reputation: 39532

You don't have to save it to a variable first:

<?php
    $transferPostFields = array(
        '00N5000000XXXXX'
    );

    $postFields = array();
    foreach ($_POST as $key => $value) {
        if (in_array($key, $transferPostFields)) {
            $postFields[$key] = $value;
        }
    }

    $curlHandle = curl_init();
    curl_setopt_array($curlHandle, array(
        CURLOPT_URL => 'http://api.salesforce.com/whatever/urls/they/use',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => http_build_query($postFields)
    ));
    $output = curl_exec($curlHandle);

    echo 'The output we received from SalesForce was: ' . $output;
?>

If you want to transfer all post fields, simply change the top part (anything above $curlHandle = curl_init() to:

$postFields = $_POST;

If you don't need to go past your own server first, then simply change your form:

<form method="post" action="http://api.salesforce.com/whatever/urls/they/use">

Upvotes: 3

Related Questions