如何将HTML头文本文件放入PHP中的变量?

时间:2014-12-29 17:53:46

标签: php html ajax json

我的html文件看起来像这样

 <form action="cnvrt.php" method="POST" >
        Enter your text here - <input type="text" id="in" name="in"> <br>
        <input type="submit" value="submit" >
    </form>

我的php文件是这样的

$in = $_POST['in'];
$url = "https://translate.google.com/translate_a/single?client=t&sl=auto&tl=si&hl=en&dt=bd&dt=ex&dt=ld&dt=md&dt=qc&dt=rw&dt=rm&dt=ss&dt=t&dt=at&ie=UTF-8&oe=UTF-8&prev=btn&rom=1&ssel=0&tsel=0&tk=517882|775443&q=$in"; 
 header( "Location: $url" );

然后我将html响应作为f.txt文件。我需要将此文件的内容保存到保存文件的变量中。

我怎么能这样做?是否可以这样做?

1 个答案:

答案 0 :(得分:0)

未经测试,但您可以执行以下操作:

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['in'])) {

    $in = $_POST['in'];

    $url = "https://translate.google.com/translate_a/single?client=t&sl=auto&tl=si&hl=en&dt=bd&dt=ex&dt=ld&dt=md&dt=qc&dt=rw&dt=rm&dt=ss&dt=t&dt=at&ie=UTF-8&oe=UTF-8&prev=btn&rom=1&ssel=0&tsel=0&tk=517882|775443&q=".$in;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $your_var = curl_exec($ch);
    curl_close($ch);
}
?>