Getting/Parsing XML data from an API that is done in PHP

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

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

?>

链接地址: http://www.djcxy.com/p/6088.html

上一篇: 谷歌地图infowindow不再显示

下一篇: 从在PHP中完成的API获取/解析XML数据