简体   繁体   中英

cURL form cant show the information (very basic)

Hello people at StackOverflow,

I am trying to learn some more on cURL and have come up with the following form:

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

And this is my receiving end script:

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

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

echo ($_POST['sender']);


?>

It is not showing my echo. Even though I think cURL is enabled on both servers.

I've edited your code slightly to add a CURLOPT_RETURNTRANSFER, that way you can receive the output of remote server as a response from the curl_exec() function. Another thing i did is to change the $sender and $receiver vars with the appropriate $_POST array indexes. Last change is that i changed the CURLOPT_POSTFIELDS directive to receive an array.

The code runs fine and the response is as expected: Just paste this into some new file and load in browser and try for yourself.

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

I also got the response upon the submit :

string(41) "Greetings, visitor from 84.229.122.167me"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM