Chris Watts
Chris Watts

Reputation: 6715

Sending POST data to a website with PHP

I'm intercepting a PayPal button click from my forum website (which has no PHP abilities) so I can process extra security information using my own server (which does have PHP).

Once I'm finished processing, I wish to send the user back off to PayPal with their request data. Currently I'm just using a fairly large GET query string in the Location header as there is no sensitive data being sent. This works perfectly fine.

I wish to use POST purely so it doesn't spam the user's address bar with stuff. I've heard that you can use cURL for this, but is that a really good idea for an HTTPS connection to PayPal?

Any suggestions appreciated.

Upvotes: 1

Views: 598

Answers (1)

Hauke Haien
Hauke Haien

Reputation: 44

Well you could use CURL

/* Some params you wish to send */
$sEncodedParams = 'foo=Bar%20Baz&key=1234';

/* Create a new CURL Object */
$ch = curl_init();

/* Set the URL */
curl_setopt($ch, CURLOPT_URL, $sUrl);

/* Make CURL use POST */
curl_setopt($ch, CURLOPT_POST, true);

/* Add the POST-Data */
curl_setopt($ch, CURLOPT_POSTFIELDS, $sEncodedParams);

/* SET the Headers */
curl_setopt($ch, CURLOPT_HEADER, 0);

/* Execute the request */
curl_exec($ch);

/* Close CURL */
curl_close($ch);

You can even get the response returned into a variable using something like this

/* Tell CURL to return the response into a variable instead of echoing it */
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

/* Save the response */
$sResponse = curl_exec($ch);

Hope this helps

Regards

Edit: In addition to SSL-Support (without CURL):

/* Timeout */
$iTimeout = 30;

/* EOL style */
$sEOL = "\r\n";

/* Some params you wish to send */
$sEncodedParams = 'foo=Bar%20Baz&key=1234';

/* Initialize the SSL-Socket */
$rSSLSocket = @fsockopen("ssl://www.domain.com", 443, $iError, $sError, $iTimeout);

/* Will contain the response data */
$sResponse = '';

/* If it worked */
if($rSSLSocket !== false) {

    /* Put whatever you need here */
    fputs($rSSLSocket, 'POST /path/here HTTP/1.0' . $sEOL);
    fputs($rSSLSocket, 'Host: www.domain.com' . $sEOL);
    fputs($rSSLSocket, 'Content-type: application/x-www-form-urlencoded' . $sEOL);
    fputs($rSSLSocket, 'Content-Length: ' . strlen($sEncodedParams) . $sEOL);
    fputs($rSSLSocket, 'Connection: close' . $sEOL . $sEOL);
    fputs($rSSLSocket, $sEncodedParams);

    while(!feof($rSSLSocket)) {
        $sResponse .= @fgets($rSSLSocket, 2048);
    }
    fclose($rSSLSocket);
} else {
    // ERROR
}

I hope this helps. Be careful though as fsockopen can be tricky

Upvotes: 1

Related Questions