#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2

    Passing GET variables through url and parsing xml


    So one of the last things I'm setting up with this shopping cart that I created to function using PHP is to get a shipping cost from the shipping companies custom rate calculator.

    Here is a couple sentences from the instructions on how to make a call to the shipping companies server in order to get the proper shipping cost,

    The custom rate calculator works in a similar manor as the base rate calculator, except that there are
    only two GET variables that are passed to the URL – “shipment” and “token”. The “shipment” variable
    contains XML tags with the shipment values set via arguments in the tag, and the “token” is what is used
    to confirm which account number to display the rates for.

    They provide an example of what the url would look like when passing the variables, (I switched the actual shipping company name with shipping co)

    Code:
    https://www.shippingco.com/XML/RatingXML.jsp?shipment=<shipment weight_system="IMPERIAL"
    shipper_number="99999999" destination_postal_code="M1M1M1" service_type="1"><total
    total_pieces="2" total_weight="20"/></shipment>&token=SHIPPINGCO
    Then they provide a sample XML result,
    Code:
    <rating xsi:noNamespaceSchemaLocation="http://www.shippingco.com/XML/rating.xsd">
    <rate weight="20.0" weight_unit="lb" zone="1" transit_time="1"
    transit_time_guaranteed="false" estimated_delivery_date="2011-11-07" base_charge="17.39"
    freight_charge="17.39" net_charge="17.39" fuel_surcharge_rate="15.3" fuel_surcharge="2.66"
    subtotal_charge="20.05" on_hst_charge="2.61" total_charge="22.66"/>
    </rating>
    So I've never done this before and I'm not really sure how this is supposed to work, should I update the header with the url they provide as an example except replacing the variables as necessary to receive the shipping cost and then once the url is sent, how do I get sent back to the shopping cart with the xml variables and parse the total shipping cost to be put into a php variable.

    OR

    Should I have some sort of xml code laid out to hold the variables that need to be passed through the url and then capture the total shipping cost variable into the same xml code and then parse it somehow. And also is there a way to pass the variables through the url without having to refresh the shopping cart page, I'm just not sure how to pass those variables through a url without actually going to the url and then how would the page go back to the shopping cart.

    Here are the variables I have now on the page,
    PHP Code:
    $total_pieces'2'// for example
    $total_weight $get_row['weight']*$total_pieces// gets the weight of of each item from the db and multiples by the quantity of the item.
    $weight_system 'IMPERIAL';
    $destination_postal_code $data5['zip']; // Grabs the zip code of the user from the db
    $service_type '1';
    $shipper_number '000222000';

    // Get Variables for Shipping Company
    $deliveryValue $_GET[$destination_postal_code];
    $shipperValue $_GET[$shipper_number];
    $serviceValue $_GET[$service_type];
    $piecesValue $_GET[$total_pieces];
    $weightValue $_GET[$total_weight];
    $weightValue2 $_GET[$weight_system];
    // End get variables for Shipping Company 
    Is this how the GET variables need to be laid out in order to pass them through the url?


    I talked to tech support for the shipping company to find help with this but they keep telling me to pass the instructions on to my web developer. I am the web developer.. and I've never done this before but would like to learn how so maybe someone here has done this and can give me some help. Any help and direction is appreciated! Thanks in advance.
    Last edited by andy1212; July 11th, 2013 at 07:12 PM.
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    You wouldn't be sending the user to the URL, you would be using PHP to issue an HTTP request to it. For most APIs you would use cURL to do this, but this particular API looks very simple and you can probably use file_get_contents or simplexml_load_file to send the request instead.

    PHP Code:
    $xml '...';
    $token '...';
    $base_url 'http://...';

    $request_url $base_url '?shipment=' urlencode($xml) . '&token=' urlencode($token);
    $result file_get_contents($request_url); 

    Comments on this post

    • andy1212 agrees : Great help and direction!
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    $xml = 'so an array of the variables that need to pass through the url would go in here?';
    $token = '000222000';
    $base_url = 'https://www.shippingco.com/XML/RatingXML.jsp';

    $request_url = $base_url . '?shipment=' . urlencode($xml) . '&token=' . urlencode($token);
    $result = file_get_contents($request_url);

    Does stream_context_create need to go in there somewhere? I'm going to play around with this tomorrow and then post back here with my results. Thanks for your help!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,968
    Rep Power
    374
    stream_context would be used if they expected POST variables
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    ok thanks
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    Ok so how would I lay out the variables within $xml?

    Like this,

    $xml = array(
    'weight_system' => "IMPERIAL",
    'shipper_number' => "0000000",
    'destination_postal_code' => "".$data5['zip']."",
    'service_type' => "1"
    );

    Or like this,

    $xml = 'weight_system="IMPERIAL" shipper_number="000000" destination_postal_code="'.$data5['zip'].'" service_type="1"';

    So urlencode() puts the %% signs around each attribute.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,968
    Rep Power
    374
    look for a sample xml they require, copy it and replace their example data with your dynamic data for ex.

    <root>
    <name>Test</name>...


    you would remove Test and put your php var $name or somethings.. btw when you copy it you would do

    $xml = "";

    alternatively you could use sprintf, or a special functions to create the xml (Xml writer)

IMN logo majestic logo threadwatch logo seochat tools logo