NYCPHP Meetup

[nycphp-talk] Form Action Help

George Webb gw.nyphp at gwprogramming.com
Wed Feb 5 20:27:38 EST 2003


> 
>  If I can refer to George's response, I think my confusion lies in step #3:
>  "Your PHP script submits a https/POST request to PayPal (or wherever)."
> 
>  How do I write the command to submit that https/POST request to PayPal?

Bill,

	Below is some code I wrote recently to do a similar thing -- basically
to re-post incoming $_POST data to "instantservice.com".  It uses cURL, which
seems to be a cool thing to do, except some PHP installations don't have it
by default.  I hope that's not you.

	Anyway, I could explain this further, but I got another crisis right
now, so I must be brief.  The response from the foreign server (InstantService)
is simply a 3-digit result code, e.g. 100.  If the transmission is successful,
the global var $ISStatus is set to this value, and my PHP function returns true. 
If the foreign server couldn't be reached, $ISStatus is left unset, and the
PHP function returns false.  Basic stuff.

	Using PayPal instead of InstantService, you may have to do more elaborate
parsing of the result.  But that's another topic!  Let me know how it goes.


Best, George.

George Webb
gw.nyphp at gwprogramming.com


<?php

/* Constants which you may change */
define ( 'INSTANTSERVICE_POST_URL', 'http://admin.instantservice.com/servlet/MailMessagePost' );
define ( 'INSTANTSERVICE_STATUS_OK', 100 );
define ( 'INSTANTSERVICE_RETRIES', 2 );
define ( 'INSTANTSERVICE_SLEEP', 4 );

function transmit () {
        /* Transmits data via HTTP to InstantService.  Returns true if successful;
        ** otherwise, returns false ans sets global $ISStatus to the response code
        ** from InstantService.
        */
        GLOBAL $ISStatus;

        /* Fake certain fields unless they already exist */
        if ( ! isset ($_POST['fromalias']) )
                $_POST['fromalias'] = trim ( "{$_POST['fname']} {$_POST['lname']}" );

        /* Build post data */
        $postfields_string = ''; // initialize
        foreach ( $_POST as $key=>$val ) {
                $postfields_string .= urlencode($key) . '='
                 . urlencode( $val ) . '&';
        }

        /* Try to connect to InstantService */
        $retries = 0;
        while (true) {
                /* Build the request */
                $ch = curl_init ( INSTANTSERVICE_POST_URL );
                curl_setopt ( $ch, CURLOPT_POST, 1);
                curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt ( $ch, CURLOPT_TIMEOUT, 30 );       // tem suggested
                curl_setopt ( $ch, CURLOPT_POSTFIELDS, $postfields_string );

                /* Make the HTTP(s) transaction */
                $ISStatus = curl_exec ($ch);
                curl_close ( $ch );

                /* Check status */
                if ( $ISStatus == INSTANTSERVICE_STATUS_OK ) {
                        return true;
                }

                /* Else sleep and maybe retry */
                if ( $retries++ >= INSTANTSERVICE_RETRIES ) return false; // transmit() failed
                else sleep ( INSTANTSERVICE_SLEEP ); // retry
        }
}




More information about the talk mailing list