duanshan1856 2018-07-17 15:28
浏览 39
已采纳

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

I am building a WordPress shopping cart plugin that will tie into our server through a URL that will produce an XML page, which I will then have to parse the info to the WordPress page.

The server contains a session id (ShoppingCart.SessionID) that I need to get and use to help store which button was clicked and then pull the product info and add it to the cart. Each button is given a product id via shortcode (product_id="some#") from the page owner and that ID is pulled from the server to display the product's info.

How the buttons are set up:

In the shortcode file, I am using a function and have the button set up as

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

In the main plugin file:

This file handles the printing of the button and the function to display the cart

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

Here is the function for what is supposed to be displaying the cart/product info:

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
",
                    '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;
}

What the XML page looks like with an example product

Not sure if this is needed, but the more info provided, the better (right?). The product ID that is sent will populate these fields and keep adding a new row and then I will parse the necessary fields to the WordPress page.

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

I am pretty new to PHP and learning as I go (reading/research), but have not been able to find the solution or steps to tackle my issue. Essentially, I believe my question should be (in short):

How can I grab the ShoppingCart.SessionID from the 3rd party server and use it to populate the cart without refreshing the Session so more than one product will display when more than one button is clicked? Also, is there a better/more productive way to accomplish what I am trying to do?

Thanks in advance and let me know if there's any info I need to add to the question!

  • 写回答

1条回答 默认 最新

  • douwen7905 2018-08-14 15:51
    关注

    In case anyone comes across this with a similar issue:

    My way around this was to send the button data to an <iframe> and then set the <iframe> to width="0" height="0".

    Example code:

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

    Not sure if this is THE BEST way to do it, but it works and I was able to add some JQuery to manipulate things even further (I tried using AJAX before this, but the server was set to "same origin", so the data wouldn't send from another URL).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器