1

大家好 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。

4

1 回答 1

1

我已经稍微编辑了您的代码以添加 CURLOPT_RETURNTRANSFER,这样您就可以接收远程服务器的输出作为 curl_exec() 函数的响应。我做的另一件事是使用适当的 $_POST 数组索引更改 $sender 和 $receiver 变量。最后一个变化是我改变了 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 的访客"

于 2013-01-02T21:54:06.493 回答