使用输入值填充cURL脚本$ url

时间:2017-07-21 16:27:57

标签: php forms curl

我有一个我正在使用的表单模板。我处理表单并使用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无济于事。我错过了什么?

1 个答案:

答案 0 :(得分:0)

试用此代码:

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