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 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题