簡體   English   中英

PHP填充HTML表單中的鏈接

[英]PHP to fill link from an HTML form

我已經使用PHP已有一段時間了,但是我無法圍繞如何使它工作的想法。

非常簡單-我希望我的鏈接根據發生的情況動態填充HTTP鏈接,具體取決於輸入到表單中的內容。

因此,我只是希望該HTTP鏈接動態填充(因為將需要插入用戶的電話號碼,並且需要隱藏我的API密鑰)。

我的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填充鏈接:

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

(原始鏈接https://api.clockworksms.com/http/send.aspx?key=KEY&to=441234567890&content=Hello+World

我需要告訴我的PHP填寫以表格(標簽“ SMSnumber”)輸入的部分。 我認為這可能有效,但仍然使我的API密鑰暴露在HTML中。

當單擊“提交”按鈕時,如何告訴此鏈接發生?

如何隱藏鏈接本身,以使我的API對公眾不可見?

謝謝

您需要設置HTTP方法。 http://www.w3schools.com/tags/att_form_method.asp

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

要隱藏您的api密鑰,您可以執行以下操作。 通過使用Curl 當然,您必須在下面的代碼中添加API密鑰。

<?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 } ?>

如果您不想公開您的API密鑰,則必須像這樣從后端發送請求:

<?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>

將您的form更改為

<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 echo $_POST["SMSnumber"]; ?> <?php echo $_POST["SMSnumber"]; ?> inputname屬性應為SMSnumber

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM