2011-10-06 106 views
0

我想讓PHP腳本將用戶發送到已經預先配置好的PayPal頁面。基本上,我試圖擺脫中間頁面,「請等待,我送你到貝寶,等等」。使用PHP與fsockopen模擬PayPal _xclick

Currenly這是正在發生的事情:1。 用戶填寫其被POST'd我process.php頁 2.我想process.php建立_xclick串並直接張貼到貝寶和表演形式(重定向?)瀏覽器中的頁面。

這就是我現在正在做的事情,但是用戶的網絡瀏覽器沒有重定向。我知道我可以回顯一些HTML並使其起作用,但我認爲有一種方法可以獲取數據,但我想讓瀏覽器採取更多行動?

//create array of requuired minimal data for a PayPal button 
$post_data['amount']  = '123'; 
$post_data['item_name']  = 'widget'; 
$post_data['item_number'] = '123'; 
$post_data['quantity'] = '123'; 
$post_data['currency_code'] = 'USD'; 
$post_data['business'] = '[email protected]'; 
$post_data['no_shipping'] = '2'; 

//traverse array and prepare data for posting (key1=value1) 
foreach ($post_data as $key => $value) 
{ 
    $post_items[] = $key . '=' . urlencode(stripslashes($value)); 
} 

//create the final string to be posted using implode() 
$post_string = implode ('&', $post_items); 

// Add command string 
$post_string = '?cmd=_xclick&' . $post_string; 

// Connect to PAYPAL 
$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30); 

//sending the data 
fputs($fp, "POST /cgi-bin/webscr HTTP/1.1\r\n"); 
fputs($fp, "Host: www.paypal.com\r\n"); 
fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n"); 
fputs($fp, "Content-Length: ".strlen($post_string)."\r\n"); 
fputs($fp, "Connection: close\r\n"); 
fputs($fp, "\r\n"); 
fputs($fp, $post_string); 

while (!feof($fp)) 
{ 
    echo fgets($fp, 1024); 
} 
fclose($fp); 

回答

1

答案是,至少在PayPal下,這是無法完成的。如果我執行一個POST,然後是GET,那麼瀏覽器URL不會更改,這會違反安全標準。簡而言之,瀏覽器(用戶)必須參與POST以保持所有正確的外觀。 PayPal提供的API可以在幕後執行所有操作,不需要HTML FORMS,但這不適用於_xclick

因此,您需要使用JavaScript並自動提交FORM。例如:

<html> 
<head> 
<title>Proceeding to credit card site ...</title> 
<body onload="document.paypal_form.submit();"> 
<h1>Proceeding to credit card site ...</<h1> 
<form method="post" name="paypal_form" action="<?php echo $post_data['url']?>"> 
<input type="hidden" name="cmd" value="<?php echo $post_data['cmd']?>" /> 
<input type="hidden" name="business" value="<?php echo $post_data['business']?>" /> 
<input type="hidden" name="currency_code" value="<?php echo $post_data['currency_code']?>" /> 
<input type="hidden" name="amount" value="<?php echo $post_data['amount']?>" /> 
<input type="hidden" name="item_name" value="<?php echo $post_data['item_name']?>" /> 
</form> 
</body> 
</html> 
+1

要添加;這不僅僅是PayPal:你無法將服務器端POST發送到某個地方,然後將買家重定向到某個地方,因爲買家不會被綁定到之前發送的POST數據。正如你所提到的,快速結賬是最好的解決方案。 – Robert