0

I am installing a UPS shipping calculator API on my webstore, and it is getting prices correctly, which is great! I can't find any way to get those prices to a useful format though. I want to use them as a php variable.

For this example, we will call my cart cart.php.

The api is being executed by a file called ups.php.

When a customer puts items in their cart and fills in their zip code, they click a submit button, and they are directed to a new cart page that runs the ups.php script and displays the data on the page (which I will remove later so it can run in the background). If any of these files were .xml files I could probably learn how to parse xml data or use simpleXML commands in php... but they always seem to want a location of an xml file, which I don't have. Does anyone know how to get the XML data contained in one of my PHP files to be usable as a PHP variable?

Here is some of the code from ups.php:

    <?php
      $ch = curl_init("https://www.ups.com/ups.app/xml/Rate");
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch,CURLOPT_POST,1);
        curl_setopt($ch,CURLOPT_TIMEOUT, 60);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
        $result=curl_exec ($ch);
    echo ''. $result. '';
        $data = strstr($result, '<?');
        $xml_parser = xml_parser_create();
        xml_parse_into_struct($xml_parser, $data, $vals, $index);
        xml_parser_free($xml_parser);
        $params = array();
        $level = array();
        foreach ($vals as $xml_elem) {
         if ($xml_elem['type'] == 'open') {
        if (array_key_exists('attributes',$xml_elem)) {
             list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
        } else {
             $level[$xml_elem['level']] = $xml_elem['tag'];
        }
         }
         if ($xml_elem['type'] == 'complete') {
        $start_level = 1;
        $php_stmt = '$params';  
        while($start_level < $xml_elem['level']) {
             $php_stmt .= '[$level['.$start_level.']]';
             $start_level++;
        }
        $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
        eval($php_stmt);
         }
        }
        curl_close($ch);
        return $params['RATINGSERVICESELECTIONRESPONSE']['RATEDSHIPMENT']['TOTALCHARGES']['MONETARYVALUE'];
    }
?>

And here is what the text output looks like on my page when I run this .php file:

HTTP/1.1 100 Continue HTTP/1.1 200 OK Date: Wed, 25 Jul 2012 20:27:46 GMT 
Server: Apache X-Frame-Options: SAMEORIGIN Pragma: no-cache 
Content-Length: 1589 
X-Powered-By: Servlet/2.5 JSP/2.1 
Vary: User-Agent Content-Type: application/xml Bare Bones Rate Request1.00011Success03Additional Handling has automatically been set on Package 1.Your invoice may vary from the displayed reference ratesLBS120.0USD65.56USD8.50USD74.06USD65.56USD8.50USD74.06120.0LBS120.0
Alfabravo
  • 7,493
  • 6
  • 46
  • 82
j G
  • 3
  • 4
  • Try `echo "
    "; print_r($data_that_was_returned);` to get a formatted array so you can see the formatted data.
    – David Jul 25 '12 at 20:53
  • I'd suggest changing the `CURLOPT_HEADER` setting to `0` - those headers aren't really useful since you know the connection is working. – John C Jul 26 '12 at 00:51
  • David- echo/print seems like a fine way to do this, but I don't know what data to reference. $data_that_was_returned isn't a variable that is in my code, so I'm guessing you want me to replace that with a variable in my code, but your syntax is PHP, and my XML rate code doesn't have many PHP variables in it. I'm a self taught newb at code, and i'm decent at html and PHP, and a hack at best when it comes to XML. – j G Jul 26 '12 at 15:08
  • John C- Thank you very much for that suggestion. That cut a lot of clutter out of the response from the UPS database. I appreciate your help. – j G Jul 26 '12 at 15:09

1 Answers1

0

Well I got it figured out. here is the new code... Way shorter. way more simple.

I used php commands for explode(); and just normal array syntax, $newvariable = $existing_array [key#];

Thanks for the comments guys!

    <?php
    $ch = curl_init("https://www.ups.com/ups.app/xml/Rate");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch,CURLOPT_TIMEOUT, 60);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
    $result=curl_exec ($ch);                    
    $upsout = (explode('USD', $result, 15));
    $handling = $upsout[3]; 
    echo "Shipping cost is $";
    echo $handling; 
    curl_close($ch);
}

?>

j G
  • 3
  • 4