Ycon
Ycon

Reputation: 1950

PHP to fill link from an HTML form

I've been around PHP for a while but I cant' wrap my head around how to get this working.

Very simple- I want my link to fill an HTTP link dynamically depending on what happened depending on what was inputted into my form.

So I simply want this HTTP link to fill dynamically (as the user's phone number will need to be inserted, and my API key will need to be hidden).

My HTML

<form>
<label for="Name">Phone Number</label><br>
<input type="SMSnumber" id="name" required><br>
<input type="submit" value="Send me the SMS">

PHP to fill link:

https://api.clockworksms.com/http/send.aspx?key=APIKEY&to=<?php echo $_POST["SMSnumber"]; 
?>&content=Hello+World

(original link https://api.clockworksms.com/http/send.aspx?key=KEY&to=441234567890&content=Hello+World)

I need to tell my PHP to fill in the section which is inputted in the form (label "SMSnumber"). I think that may work but it still leaves my API key exposed in HTML.

How can I tell this link to happen when the "submit" button is clicked?

How can I hide the link itself so my API is not visible to the public?

Thanks

Upvotes: 1

Views: 217

Answers (3)

PHPhil
PHPhil

Reputation: 1540

You need to set the HTTP method. http://www.w3schools.com/tags/att_form_method.asp

<form method="POST">
//------^

To hide your api key you can do the following. by using Curl. Ofcourse you have to add your API-key in the code below.

<?php

if(isset($_POST['SMSnumber'])){
    $url = 'https://api.clockworksms.com/http/send.aspx?key=APIKEY&to=' . $_POST["SMSnumber"] . '&content=Hello+World';

    $ch = curl_init();

        //Set Options
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        // Execute request
        $result = curl_exec($ch);

        echo $result;

} else {

?>

<form method="POST">
<label for="Name">Phone Number</label><br>
<input name="SMSnumber" type="text" id="name" required><br>
<input type="submit" value="Send me the SMS">
</form>

<?php } ?>

Upvotes: 3

knetsi
knetsi

Reputation: 1631

If you don't want to expose your API key then the request must be sent from the backend like this:

<?php

  if(isset($_POST['SMSnumber']){
    $url="https://api.clockworksms.com/http/send.aspx?key=APIKEY&to=".$_POST["SMSnumber"]."&content=Hello+World";

    $curl = curl_init();
    curl_setopt_array($curl, array(
         CURLOPT_RETURNTRANSFER => 1,
         CURLOPT_URL => $url,
         CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3'
       ));
    $resp = curl_exec($curl);
    curl_close($curl);
  }
?>
<form method="POST">
 <label for="Name">Phone Number</label><br>
 <input type="text" name='SMSnumber' id="name" required><br>
 <input type="submit" value="Send me the SMS">
</form>

Upvotes: 1

Dhinju Divakaran
Dhinju Divakaran

Reputation: 893

Change you form to

<form method="post">
<label for="Name">Phone Number</label><br>
<input name="SMSnumber" type="text" id="name" required><br>
<input type="submit" value="Send me the SMS">
</form>

Because You used <?php echo $_POST["SMSnumber"]; ?> to this name attribute of input should be SMSnumber

Upvotes: 0

Related Questions