我正在向USPS发送XML脚本,它以XML格式回复我的屏幕。我不希望它显示,我希望将响应写入像“usps_responce.xml”这样的文件,以便我以后可以使用它。在下面的示例中,如果我将XML脚本直接复制并粘贴到我的浏览器URL中,它可以很好地工作,但正如我所说,我希望它写入文件而不是屏幕。我尝试了多种方法,这是迄今为止我提出的最好的方法,我想。谢谢你的帮助
<?php
$xml = "http://production.shippingapis.com/ShippingApi.dll?API=RateV4&XML=
<RateV4Request USERID="my_user_id">
<Revision>2</Revision>
<Package ID="1ST">
<Service>ALL</Service>
<ZipOrigination>99016</ZipOrigination>
<ZipDestination>99207</ZipDestination>
<Pounds>40</Pounds>
<Ounces>1</Ounces>
<Container/>
<Size>REGULAR</Size>
<Machinable>true</Machinable>
<DropOffTime>17:00</DropOffTime>
<ShipDate>2018-01-19</ShipDate>
</Package>
<Package ID="2ND">
<Service>ALL</Service>
<ZipOrigination>99016</ZipOrigination>
<ZipDestination>99207</ZipDestination>
<Pounds>5</Pounds>
<Ounces>1</Ounces>
<Container/>
<Size>LARGE</Size>
<Width>7</Width>
<Length>7</Length>
<Height>7</Height>
<Girth>28</Girth>
<Machinable>true</Machinable>
<DropOffTime>17:00</DropOffTime>
<ShipDate>2018-01-19</ShipDate>
</Package>
</RateV4Request>";
file_put_contents("usps_responce.xml", $xml);
$xml2 = file_get_contents("usps_responce.xml");
echo $xml2;
?>
如果我将以下脚本复制并粘贴到我的网络浏览器中
http://production.shippingapis.com/ShippingApi.dll?API=RateV4&XML=
<RateV4Request USERID="my_user_id">
<Revision>2</Revision>
<Package ID="1ST">
<Service>ALL</Service>
<ZipOrigination>99016</ZipOrigination>
<ZipDestination>99207</ZipDestination>
<Pounds>40</Pounds>
<Ounces>1</Ounces>
<Container/>
<Size>REGULAR</Size>
<Machinable>true</Machinable>
<DropOffTime>17:00</DropOffTime>
<ShipDate>2018-01-19</ShipDate>
</Package>
<Package ID="2ND">
<Service>ALL</Service>
<ZipOrigination>99016</ZipOrigination>
<ZipDestination>99207</ZipDestination>
<Pounds>5</Pounds>
<Ounces>1</Ounces>
<Container/>
<Size>LARGE</Size>
<Width>7</Width>
<Length>7</Length>
<Height>7</Height>
<Girth>28</Girth>
<Machinable>true</Machinable>
<DropOffTime>17:00</DropOffTime>
<ShipDate>2018-01-19</ShipDate>
</Package>
</RateV4Request>
然后USPS回复到我的屏幕,其结果如下,类似于以下结果。注意Rate标签,这是我需要的变量之一。
<RateV4Response>
<Package ID="1ST">
<ZipOrigination>99016</ZipOrigination>
<ZipDestination>99207</ZipDestination>
<Pounds>40</Pounds>
<Ounces>1</Ounces>
<Size>REGULAR</Size>
<Machinable>TRUE</Machinable>
<Zone>1</Zone>
<Postage CLASSID="3">
<MailService>Priority Mail Express 2-Day<sup>™</sup></MailService>
<Rate>100.20</Rate>
<CommitmentDate>2018-01-22</CommitmentDate>
<CommitmentName>2-Day</CommitmentName>
</Postage><Postage CLASSID="2">
答案 0 :(得分:1)
php file_get_contents()函数从文件中查询数据,而是将文本字符串放在需要放置文件路径的位置。 将该xml字符串设置为变量,然后将其用作file_put_contents()中的参数 E.g:
$xml ="your xml string goes here";
$name_of_file = "ups_response.xml";
$file = file_put_contents($name_of_file, $xml);