繁体   English   中英

PHP使用由第三方服务器/ URL设置的会话cookie ID

[英]PHP Using a Session cookie ID set by 3rd party server/URL

我正在构建一个WordPress购物车插件,该插件将通过URL绑定到我们的服务器,该URL会生成一个XML页面,然后我必须将其解析为WordPress页面的信息。

服务器包含一个session id (ShoppingCart.SessionID) ,我需要获取它并用来帮助存储单击的按钮,然后拉出产品信息并将其添加到购物车。 页面所有者通过shortcode (product_id="some#")为每个button提供了一个product id ,并从服务器提取了该ID以显示产品信息。

按钮的设置方式:

在简码文件中,我正在使用一个函数,并将按钮设置为

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

在主插件文件中:

该文件处理按钮的打印和显示购物车的功能

<?php
session_id();
session_start();

// This function prints the button with its assigned $product_id

function print_add_cart_button_for_product($product_id, $atts = array()) {
    $replacement .= '<form method="POST" class="cart-button-form" style="display:inline" action="">';
    $replacement .= '<input type="hidden" name="bmt_cart_product" value="' . $product_id . '" />';

    $replacement .= '</form>';
    $replacement .= '</div>';
    return $replacement;
}

这是应该显示购物车/产品信息的功能:

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

示例产品的XML页面的外观

不知道是否需要这样做,但是提供的信息越多越好(对吗?)。 发送的产品ID将填充这些字段并继续添加新行,然后我将必要的字段解析到WordPress页面。

<shoppingcart sessionid="88582813">
  <producttable>
   <row number="0">
    <productid>22804</productid>
    <productname>XFree86 CD</productname>
    <quantity>1</quantity>
    <productprice>$15.00</productprice>
    <discount>$0.00</discount>
    <rowtotal>$15.00</rowtotal>
   </row>
  </producttable>
 </shoppingcart>

我对PHP和学习(阅读/研究)很陌生,但是却找不到解决问题的解决方案或步骤。 本质上,我认为我的问题应该(简而言之):

如何从第三方服务器获取ShoppingCart.SessionID并使用它填充购物车而无需刷新Session,因此单击多个按钮时将显示多个产品? 另外,是否有更好/更有效的方式来完成我要尝试的工作?

在此先感谢您,让我知道是否需要添加任何信息!

如果有人遇到类似问题:

我的解决方法是将button数据发送到<iframe> ,然后将<iframe>设置为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,但是服务器设置为“相同来源”,因此数据不会从其他网址发送)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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