PHP使用由第三方服务器/URL设置的会话cookie ID
我正在构建一个WordPress购物车插件,该插件将通过一个URL连接到我们的服务器,该URL将生成一个XML页面,然后我将不得不将该信息解析到WordPress页面 服务器包含一个PHP使用由第三方服务器/URL设置的会话cookie ID,php,wordpress,session-cookies,Php,Wordpress,Session Cookies,我正在构建一个WordPress购物车插件,该插件将通过一个URL连接到我们的服务器,该URL将生成一个XML页面,然后我将不得不将该信息解析到WordPress页面 服务器包含一个会话id(ShoppingCart.SessionID),我需要获取并使用它来帮助存储单击的按钮,然后提取产品信息并将其添加到购物车。每个按钮通过页面所有者的快捷码(product_id=“some#”)获得一个产品id,该id从服务器中提取以显示产品信息 按钮的设置方式: function show_shoppin
会话id(ShoppingCart.SessionID)
,我需要获取并使用它来帮助存储单击的按钮,然后提取产品信息并将其添加到购物车。每个按钮
通过页面所有者的快捷码(product_id=“some#”)
获得一个产品id
,该id从服务器中提取以显示产品信息
按钮的设置方式:
function show_shopping_cart_handler($atts) {
$output = "";
$form = '';
$output .= '<table style="width: 100%;">';
$output .= '<tr class="cart_item_row">
<th class="cart_item_name_th">' . (__("Item Name", "wordpress-simple-paypal-shopping-cart")) . '</th><th class="cart_price_th">' . (__("Price", "wordpress-simple-paypal-shopping-cart")) . '</th><th></th>
</tr>';
# For now, I am using this to print the headers that display the
# ShoppingCart.SessionID="somevalue", but on every button click it changes the value of the session id.
if (isset($_POST['add_cart_submit'])) {
// The server reads the ID from "&PRODUCTID=product#"
$id = "&PRODUCTID=" . $_POST['cart_product'];
$url = "https://secure.bmtmicro.com/cart?CID=2/WP" . $id;
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'GET',
'content' => http_build_query($data)
)
);
//$context = stream_context_create($options);
$context = stream_context_set_default($options);
$output .= file_get_contents($url, false, $context);
}
print_r(get_headers($url));
$output .= '</table>';
return $output;
}
在shortcode文件中,我使用了一个函数,并将按钮设置为
<?php
add_shortcode('add_cart_button', 'add_cart_button_handler');
function add_cart_button_handler($atts) {
extract(shortcode_atts(array(
'product_id' => '',
), $atts));
return print_add_cart_button_for_product($product_id, $atts);
}
如果有人遇到类似问题:
function show_shopping_cart_handler($atts) {
$output = "";
$form = '';
$output .= '<table style="width: 100%;">';
$output .= '<tr class="cart_item_row">
<th class="cart_item_name_th">' . (__("Item Name", "wordpress-simple-paypal-shopping-cart")) . '</th><th class="cart_price_th">' . (__("Price", "wordpress-simple-paypal-shopping-cart")) . '</th><th></th>
</tr>';
# For now, I am using this to print the headers that display the
# ShoppingCart.SessionID="somevalue", but on every button click it changes the value of the session id.
if (isset($_POST['add_cart_submit'])) {
// The server reads the ID from "&PRODUCTID=product#"
$id = "&PRODUCTID=" . $_POST['cart_product'];
$url = "https://secure.bmtmicro.com/cart?CID=2/WP" . $id;
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'GET',
'content' => http_build_query($data)
)
);
//$context = stream_context_create($options);
$context = stream_context_set_default($options);
$output .= file_get_contents($url, false, $context);
}
print_r(get_headers($url));
$output .= '</table>';
return $output;
}
我的解决方法是将按钮数据发送到
,然后将
设置为width=“0”height=“0”
示例代码:
<form method="POST" action="URL" target="the_iframe">
// add some <input> fields data
</form>
<iframe type="hidden" name="the_iframe" width="0" height="0"></iframe>
//添加一些字段数据
不确定这是否是最好的方法,但它很有效,我能够添加一些JQuery来进一步操作(在此之前我尝试使用AJAX,但服务器设置为“同一来源”,因此数据不会从另一个URL发送).为什么您要使用第三方会话id。只使用您自己的会话id不是更简单吗?我假设您不能修改会话数据(因为它是第三方的)。显然,服务器存储了自己的会话cookie。我最初的计划是获得一个API密钥并发出请求,但这显然是我被告知要走的路线……我仍然不确定你有什么问题。您需要会话id来查询商店API还是管理用户篮?我相信用户篮。一旦他们有了自己的项目,他们会点击结帐,然后将他们带到我们单独的结帐处,因此(如果我能正确地想到这一点),所选项目仍应存储在我们服务器的会话中,并在您进行切换时直接传递到结帐页,您需要传递会话id。
<form method="POST" action="URL" target="the_iframe">
// add some <input> fields data
</form>
<iframe type="hidden" name="the_iframe" width="0" height="0"></iframe>