【问题标题】:Populate cURL script $url with input value使用输入值填充 cURL 脚本 $url
【发布时间】:2017-07-21 16:27:57
【问题描述】:

我有一个正在使用的表单模板。我处理表单并使用 cURL 发送数据。提交的端点因表单而异,因此我尝试将其放在页面上的 var 中,用值填充隐藏的输入并以这种方式提交。只有当我在 cURL 脚本中硬编码 $url var 时它才有效。我正在使用 $_POST 输入值填充其他字段,不知道为什么它不起作用。

在实际的表单页面上,我是这样设置的:

<?php
    $inf_endpoint = 'http://someuniqueurl.com';
?>
<html>
    <body>
         <form ...>
             <input type="hidden" id="inf_endpoint" name="inf_endpoint" value="<?php echo $inf_endpoint ?>" />
         </form>
     </body>
 </html>

在处理器/cURL 脚本中

<?php
    $url = $_POST['inf_endpoint'];
    //other field and curl stuff
    $ch = curl_init($url);
?>

它永远不会像这样提交,我必须硬编码$url 值。我试过$_POST$_GET 无济于事。我错过了什么?

【问题讨论】:

  • 您给定的网址无效。
  • 应该在我的代码中纠正这一点,我的错。但问题仍然存在,无法将 $url 值设置为每个表单的隐藏输入

标签: php forms curl


【解决方案1】:

试试这个代码:

<?php
    //$inf_endpoint = 'http://someuniqueurl.com';
    if(isset($_POST['submit'])){

        $url=$_POST['inf_endpoint'];
        //$url='https://www.google.com/';

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        $result = curl_exec($ch);
        if($result === false){
            echo 'Curl error: ' . curl_error($ch);
        }
        curl_close($ch);
        echo($result);
    }

?>

把你的html改成这个

<html>
    <body>
         <form action="" methode="post">
             <input type="url" id="inf_endpoint" name="inf_endpoint" value="" />
             <button type="submit" name="submit">Submit</button>
         </form>
     </body>
</html>

【讨论】:

  • 这仍然有一个硬编码的 $url 值,我希望我们使用相同的处理器来处理模板化表单,这将具有不同的 $url 值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-22
  • 2020-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-31
  • 1970-01-01
相关资源
最近更新 更多