Search code examples
phphttpcurlpostphp-curl

Sending data from a form to a http post request but wrong format?


I'm trying to send a post request and this is the correct format

https://domainname.com/dashboard/api?to={PHONE NUMBER}&from={SENDER ID}&message={TEXT}&email={YOUR EMAIL}&api_secret={API SECRET}

This is how the request should look like:

https://domainname.com/dashboard/api?to=123456789&from=text&message=text&email=email@email.com&api_secret=123abc

So i made a html form:

<div class="body"> 
    <form method="post" action="index.php">
        <div id="form"> 
            <div class="formInput"> 
                <label>To: 
                <input type="text" name="to" id="to" /> 
                </label> 
            </div> 
            <div class="formInput"> 
                <label>From: 
                <input type="text" name="from" id="from" /> 
                </label> 
            </div> 
            <div class="formInput"> 
                <label>Message: 
                <input type="text" name="message" id="message" /> 
                </label> 
            </div>
            <div class="formInput"> 
                <label>Email: 
                <input type="text" name="email" id="email" /> 
                </label> 
                <div class="formInput"> 
                <label>Api_Secret: 
                <input type="text" name="api_secret" id="api_secret" /> 
                </label> 
            </div> 
            </div> 
            <input type="submit" value="Submit" /> 
        </div> 
    </form>

And a php file to process the data with curl:

<?php
$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => 'https://domainname.com/dashboard/api',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => [
        'to' => $_POST['to'],
        'from' => $_POST['from'],
        'message' => $_POST['message'],
        'email' => $_POST['email'],
        'api_secret' => $_POST['api_secret'],
    ],
]);

$response = curl_exec($ch);

curl_close($ch);

echo($response);
?>

But it still doesn't work. I did a request on postbin and the query looks like this:

123456789:
text:
text:
email@email.com:
123abc:

Is it the wrong format? Am i sending everyhing in a wrong format? Help is highly appreciated as i have been messing with this for the past 3 days..


Solution

  • Your API seems to accept GET request not POST, your call with curl is POST.

    So, your form seems to be ok, they have all the variables needed by API.

    Now I think the problem is your index.php file (wich is called from the form), try this:

    <?php
    //checking for all variables filled in form
    if (isset($_POST['to']) && isset($_POST['from']) && isset($_POST['message']) && isset($_POST['email']) && isset($_POST['api_secret'])){
        //rebuild API call
        $_ENDPOINT_CALL = "https://domainname.com/dashboard/api?to={$_POST['to']}&from={$_POST['from']}&message={$_POST['message']}&email={$_POST['email']}&api_secret={$_POST['api_secret']}";
    
        //cURL GET request
        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_URL => $_ENDPOINT_CALL
        ]);
        $response = curl_exec($curl);
        curl_close($curl);
    
        //write response
        echo $response;
    }
    ?>