PHP从HTML表单填充链接

时间:2015-07-16 10:23:02

标签: php html forms

我已经在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”)。我认为这可能有效,但它仍然以HTML格式显示我的API密钥。

如果点击“提交”按钮,我怎么能告诉这个链接?

如何隐藏链接本身,以便我的API对公众不可见?

由于

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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"]; ?> name input SMSnumber属性,global $product; $formatted_attributes = array(); $attributes = $product->get_attributes(); foreach($attributes as $attr=>$attr_deets){ $attribute_label = wc_attribute_label($attr); if ( isset( $attributes[ $attr ] ) || isset( $attributes[ 'pa_' . $attr ] ) ) { $attribute = isset( $attributes[ $attr ] ) ? $attributes[ $attr ] : $attributes[ 'pa_' . $attr ]; if ( $attribute['is_taxonomy'] ) { $formatted_attributes[$attribute_label] = implode( ', ', wc_get_product_terms( $product->id, $attribute['name'], array( 'fields' => 'names' ) ) ); } else { $formatted_attributes[$attribute_label] = $attribute['value']; } } } //print_r($formatted_attributes); return $formatted_attributes;

相关问题