从PHP中完成的API获取/解析XML数据

时间:2022-10-29 19:56:18

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.

我正在我的网上商店安装UPS运费计算器API,它正在价格正确,这太棒了!我找不到任何办法让这些价格变成有用的格式。我想将它们用作php变量。

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

在这个例子中,我们将调用我的购物车cart.php。

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

api由一个名为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?

当客户将商品放入购物车并填写他们的邮政编码时,他们会点击提交按钮,然后将他们定向到运行ups.php脚本的新购物车页面并在页面上显示数据(稍后我将删除)所以它可以在后台运行)。如果这些文件中的任何一个是.xml文件,我可能会学习如何解析xml数据或在php中使用simpleXML命令......但是他们似乎总是想要一个xml文件的位置,这是我没有的。有谁知道如何让我的一个PHP文件中包含的XML数据可用作PHP变量?

Here is some of the code from ups.php:

以下是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:

以下是运行此.php文件时我的页面上的文本输出:

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

1 个解决方案

#1


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#];

我使用php命令进行explode();而且只是普通的数组语法,$ 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);
}

?>

#1


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#];

我使用php命令进行explode();而且只是普通的数组语法,$ 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);
}

?>