【问题标题】:PHP to fill link from an HTML formPHP从HTML表单中填充链接
【发布时间】:2015-07-16 10:23:01
【问题描述】:

我使用 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 对公众不可见?

谢谢

【问题讨论】:

  • 您没有告诉我们您在哪里使用该链接,如果您使用 PHP 将数据发送到该 URL,那么 API 密钥不会公开,您可以使用 cURL 库 php.net/manual/en/book.curl.php 进行服务器到服务器沟通
  • 我计划在我的主页上分享链接(公开),让用户输入他们的号码,以便通过短信发送到 AppStore 的链接。
  • 如果您在主页设置 API 密钥,则无法保护您的 API 密钥,您必须让用户发布您希望他们发布的数据,然后在您的服务器上处理请求并将数据定向到与您的 API 密钥的链接。
  • 一般来说,您需要做的是,创建一个表单,用户会将 SMSnumber 提交到您的脚本,然后在后端创建 URL 并通过 cURL 将数据发送到该 URL

标签: php html forms


【解决方案1】:

您需要设置 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 } ?>

【讨论】:

  • 谢谢 - 但这会公开显示链接。我怎样才能保持私密?当点击“提交”时,如何让该链接运行?
  • 编辑了我的asnwer,你可以用Curl获取你的api的课程代码。使用源代码,您可以做任何您想做的事情。它存储在 $result
  • 谢谢-但是“提交”按钮将我带到“test.php”页面,并且似乎并没有真正“执行”HTTP请求。
  • 我删除了 action="test.php" 并且它起作用了。谢谢菲尔博士
  • 抱歉 action="test.php" 不应该在那里,那是为了在这里测试它。编辑了我的答案。
【解决方案2】:

如果您不想公开您的 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>

【讨论】:

    【解决方案3】:

    把你的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>
    

    因为你使用了&lt;?php echo $_POST["SMSnumber"]; ?&gt;input 的这个name 属性应该是SMSnumber

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-07
      • 1970-01-01
      • 2019-10-16
      • 1970-01-01
      • 1970-01-01
      • 2017-09-02
      • 2014-07-08
      • 2011-06-24
      相关资源
      最近更新 更多