cURL表格无法显示信息(非常基本)

时间:2013-01-02 21:14:51

标签: php forms curl cross-server

StackOverflow的人们,

我正在尝试更多地了解cURL并提出以下表格:

<?php 
            //controleer of het formulier gesubmit is. Als dat zo is stuur de data door naar de link 

            if(isSet($_POST['submit'])) 
                { 
                    $ch = curl_init("http://jecom.nl/jecom/curl/receive.php");  
                    //definieer de link 

                    curl_setopt($ch, CURLOPT_POST, true); 
                     //zet het type op post 

                    curl_setopt($ch, CURLOPT_POSTFIELDS, "sender=" .$sender."&receiver=".$receiver."");  
                    //stuur de gegevens uit het formulier door naar de link 

                    curl_exec($ch);  
                    //Zet de output op het scherm 

                    if (curl_errno($ch))  
                        { 
                               print curl_error($ch); 
                               //Als er een fout is geef deze dan 
                        } 
                    else 
                        { 
                            curl_close($ch);  
                            //Sluit de link met de website 
                        } 

                } 
            else 
                { 
            ?> 
    <form method="POST" action="<? echo $_SERVER['PHP_SELF'];  ?>" name="login"> 
        <table width="100%"> 
            <tr align="center"> 
                <td width="50%" align="right"><font color="navy">Sender</font></td> 
                <td width="50%" align="left"><input type="text" name="sender" size="50"></td> 
            </tr> 

            <tr align="center"> 
                <td width="50%" align="right" width="100"><font color="navy">Receiver</font></td> 
                <td width="50%" align="left"><input type="text" size="50" name="receiver"></td> 
            </tr> 
                            <tr align="center"> 
                <td width="50%" align="right" width="100"><font color="navy">Amount</font></td> 
                <td width="50%" align="left"><input type="text" size="50" name="amount"></td> 
            </tr> 
            <tr> 
                <td>&nbsp;</td> 
            </tr> 
            <tr align="center"> 
                <td colspan="2" align="center"><input name="submit" type="submit" value="Inloggen"></td> 
            </tr> 

        </table> 
        </form> 
        <? } ?>

这是我的接收端脚本:

    <?php
// recipient.php
$sender = ($_POST["sender"]);
$receiver = ($_POST["receiver"]);
$amount = ($_POST["amount"]);

print "Greetings, visitor from  {$_SERVER['REMOTE_ADDR']}";

echo ($_POST['sender']);


?>

它没有表现出我的回声。即使我认为在两台服务器上都启用了cURL。

1 个答案:

答案 0 :(得分:1)

我稍微编辑了你的代码以添加一个CURLOPT_RETURNTRANSFER, 这样你就可以收到远程服务器的输出作为curl_exec()函数的响应。 我做的另一件事是使用适当的$ _POST数组索引更改$ sender和$ receiver vars。 最后一个更改是我更改了CURLOPT_POSTFIELDS指令以接收数组。

代码运行正常,响应符合预期: 只需将其粘贴到一些新文件中,然后在浏览器中加载并自行尝试。

  <?php

            if($_POST) {
                $ch = curl_init("http://jecom.nl/jecom/curl/receive.php");
                curl_setopt($ch, CURLOPT_POST, true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, array('sender' => $_POST['sender'], 'receiver'=> $_POST['receiver']));
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

                $result = curl_exec($ch); // HERE YOU GET THE RESULT OF THE REQUEST AS A STRING

                if (curl_errno($ch))
                {
                    print curl_error($ch);
                } else {
                    curl_close($ch);
                }
                var_dump($result);
            }
    ?>

<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];  ?>" name="login">
    <table width="100%">
        <tr align="center">
            <td width="50%" align="right"><font color="navy">Sender</font></td>
            <td width="50%" align="left"><input type="text" name="sender" size="50"></td>
        </tr>

        <tr align="center">
            <td width="50%" align="right" width="100"><font color="navy">Receiver</font></td>
            <td width="50%" align="left"><input type="text" size="50" name="receiver"></td>
        </tr>
        <tr align="center">
            <td width="50%" align="right" width="100"><font color="navy">Amount</font></td>
            <td width="50%" align="left"><input type="text" size="50" name="amount"></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
        </tr>
        <tr align="center">
            <td colspan="2" align="center"><input name="submit" type="submit" value="Inloggen"></td>
        </tr>

    </table>
</form>

我也在提交时得到了答复:

string(41)“问候,来自84.229.122.167me的访客”