简体   繁体   English

将XML数据转换为ISO-8859-1以获取API调用POST数据

[英]Converting XML data to ISO-8859-1 for API call POST data

I've got XML data that I'm using to post to the idibu API (job board API in the UK). 我有要用于发布到idibu API(英国的求职板API)的XML数据。 I've done a successful call with their test POST data. 我已经用他们的测试POST数据成功致电。 Here it is: 这里是:

xml_text=%3C%3Fxml+version%3D%221.0%22+encoding%3D%22UTF-8%22%3F%3E%0D%0A%3Cidibu%3E%0D%0A%3Cmethod%3Eadd%3C%2Fmethod%3E%0D%0A%3Cconfig%3E%0D%0A%3Cshow_durations%3Eno%3C%2Fshow_durations%3E%0D%0A%3Ccompletionurl%3Eemail%3C%2Fcompletionurl%3E%0D%0A%3Cadvertcompletionemail%3Ebob%40bob.com%3C%2Fadvertcompletionemail%3E%0D%0A%3Clockboards%3Eyes%3C%2Flockboards%3E%0D%0A%3Credirecturl%3Ehttp%3A%2F%2Fwww.google.com+%3C%2Fredirecturl%3E%0D%0A%3Cvalidate_level%3Ewarning%3C%2Fvalidate_level%3E%0D%0A%3C%2Fconfig%3E%0D%0A%3Cjob%3E%0D%0A%3Ctitle%3E%3C%21%5BCDATA%5BXML+v+3+test%2C+please+ignore+Special+%A3+%24+%26+%25+%40+%21+%3F+.+%2C+%3D+%29+%28+-+%3A+%3B+_+%2B+%27+%22+%80%5D%5D%3E%3C%2Ftitle%3E%0D%0A%3Creference%3EABC123456789%3C%2Freference%3E%0D%0A%3Cdescription%3E%3C%21%5BCDATA%5B%3Cb%3ESpecial+Te+%A3+%24+%26+%25+%40+%21+%3F+.+%2C+%3D+%29+%28+%2F+-+%3A+%3B+_+%2B+%27+%22+%27+here+euro+%80+%0D%0ABasic+Te+%A3+%24+%26+%25+%40+%21+%3F+.+%2C+%3D+%29+%28+%2F+-+%3A+%3B+_+%2B+%27+%22+%27+%80+%0D%0ASpecial+Te+%A3+%24+%26+%25+%40+%21+%3F+.+%2C+%3D+%29+%28+%2F+-+%3A+%3B+_+%2B+%27+%22+%27+%80+%0D%0A%0D%0A+%0D%0A+Special+Te+%A3+%24+%26+%25+%40+%21+%3F+.+%2C+%3D+%29+%28+%2F+-+%3A+%3B+_+%2B+%27+%22+%27+%80+%0D%0ABasic+Te+%A3+%24+%26+%25+%40+%21+%3F+.+%2C+%3D+%29+%28+%2F+-+%3A+%3B+_+%2B+%27+%22+%27+%80+%0D%0A%0D%0A+%96+%96+%92+%27+%95+++++++%2C+%09%2C+%95+%B7+%93+%94+%D8+%85+%A8+%A6+%27+%3A+%27+%83+%E1+%91+%BA+%A1+%A7+%E9+%D7+%87+%27+%3A+%27+%27+%C7+%BD+%84+h+%AE+%97+%AC+%83+%E1+%99%3C%2Fb%3E%5D%5D%3E%3C%2Fdescription%3E%0D%0A%3Csender%3E%0D%0A%3Cname%3ESteve%3C%2Fname%3E%0D%0A%3Clastname%3ERogers%3C%2Flastname%3E%0D%0A%3Cemail%3Etest%40mail.net%3C%2Femail%3E%0D%0A%3Ccompany%3EOne+World+Market%3C%2Fcompany%3E%0D%0A%3Cphone%3E44+%280%29+111-1111111%3C%2Fphone%3E%0D%0A%3Cwww%3Ehttp%3A%2F%2Fuk.idibu.com+%3C%2Fwww%3E%0D%0A%3Ccountry%3EUK%3C%2Fcountry%3E%0D%0A%3Cpostcode%3E020+1111+1111%3C%2Fpostcode%3E%0D%0A%3C%2Fsender%3E%0D%0A%3Ccategory+id%3D%2221%22+%2F%3E%0D%0A%3Clocation+id%3D%228%22+%2F%3E%0D%0A%3Csublocation+id%3D%22668%22+%2F%3E%0D%0A%3Cjobtype+id%3D%222%22+%2F%3E%0D%0A%3Cjob_time%3E2%3C%2Fjob_time%3E%0D%0A%3Cstartdate%3E2011-11-26%3C%2Fstartdate%3E%0D%0A%3Cduration%3EFull+time%3C%2Fduration%3E%0D%0A%3Csalarymin%3E20000%3C%2Fsalarymin%3E%0D%0A%3Csalarymax%3E25000%3C%2Fsalarymax%3E%0D%0A%3Csalaryper+value%3D%22annum%22+%2F%3E%0D%0A%3CsalaryOverride%3ESalary+override+test+test%3C%2FsalaryOverride%3E%0D%0A%3Ccurrency%3EGBP%3C%2Fcurrency%3E%0D%0A%3Cpublish%3E2011-11-26%3C%2Fpublish%3E%0D%0A%3Cposts%3E%0D%0A%3Cboard+id%3D%22517%22%3E%0D%0A%3Cextrafield+name%3D%22idibudts_cat%22%3E3%3C%2Fextrafield%3E%0D%0A%3Cduration+days%3D%227%22+%2F%3E%0D%0A%3C%2Fboard%3E%0D%0A%3C%2Fposts%3E%0D%0A%3C%2Fjob%3E%0D%0A%3C%2Fidibu%3E

I've been able to decode this into standard XML format here: http://coderstoolbox.net/string/#!encoding=url&action=decode&charset=us_ascii (paste the info into the top box). 我已经能够在此处将其解码为标准XML格式: http : //coderstoolbox.net/string/#! encoding= url&action= decode& charset=us_ascii (将信息粘贴到顶部框中)。 Below is what I get: 以下是我得到的:

xml_text=<?xml version='1.0' encoding='UTF-8'?>
<idibu>
<method>add</method>
<config>
<show_durations>no</show_durations>
<completionurl>email</completionurl>
<advertcompletionemail>bob@bob.com</advertcompletionemail>
<lockboards>yes</lockboards>
<redirecturl>http://www.google.com </redirecturl>
<validate_level>warning</validate_level>
</config>
<job>
<title><![CDATA[XML v 3 test, please ignore Special £ $ & % @ ! ? . , = ) ( - : ; _ + ' ' ]]></title>
<reference>ABC123456789</reference>
<description><![CDATA[<b>Special Te £ $ & % @ ! ? . , = ) ( / - : ; _ + ' ' ' here euro  
Basic Te £ $ & % @ ! ? . , = ) ( / - : ; _ + ' ' '  
Special Te £ $ & % @ ! ? . , = ) ( / - : ; _ + ' ' '  
Special Te £ $ & % @ ! ? . , = ) ( / - : ; _ + ' ' '  
Basic Te £ $ & % @ ! ? . , = ) ( / - : ; _ + ' ' '  '        ,    ,  ·   Ø  ¨ ¦ ' : '  á  º ¡ § é ×  ' : ' ' Ç ½  h ®  ¬  á </b>]]>        
</description>
<sender>
<name>Steve</name>
<lastname>Rogers</lastname>
<email>test@mail.net</email>
<company>One World Market</company>
<phone>44 (0) 111-1111111</phone>
<www>http://uk.idibu.com </www>
<country>UK</country>
<postcode>020 1111 1111</postcode>
</sender>
<category id='21' />
<location id='8' />
<sublocation id='668' />
<jobtype id='2' />
<job_time>2</job_time>
<startdate>2011-11-26</startdate>
<duration>Full time</duration>
<salarymin>20000</salarymin>
<salarymax>25000</salarymax>
<salaryper value='annum' />
<salaryOverride>Salary override test test</salaryOverride>
<currency>GBP</currency>
<publish>2011-11-26</publish>
<posts>
<board id='517'>
<extrafield name='idibudts_cat'>3</extrafield>
<duration days='7' />
</board>
</posts>
</job>
</idibu>

In interest of simplicity, I'm setting up the API data in the latter of the two formats (standard XML). 为了简单起见,我以两种格式(标准XML)中的后一种设置API数据。 For example, for the advertcompletionemail is would be: 例如,对于advertcompletionemail,将为:

echo "<advertcompletionemail>".$advert_email."</advertcompletionemail>";

The info has to be encode though in order to ultimately send it to the API once I'm done setting it up. 信息必须经过编码,以便在完成设置后最终将其发送到API。 The idibu documentation says it needs to be encoded as iso-8859-1 which is what I'm assuming the first block of code is. idibu文档说它需要被编码为iso-8859-1,这就是我假设的第一段代码。 I've tried to set the whole XML data as a string ($str) and then run: 我尝试将整个XML数据设置为字符串($ str),然后运行:

echo "htmlentities($str)";

OR 要么

echo "htmlentities($str, ENT_QUOTE, ISO-8859-1);

But neither works. 但是两者都不起作用。 I also tried utf8_decode($str) (and utf8_encode($str) even though I was pretty sure that wasn't the right function) and it didn't work. 我还尝试了utf8_decode($ str)(和utf8_encode($ str),尽管我非常确定这不是正确的功能),但是它没有用。 Can anyone help me figure out how to encode this XML to the correct format? 谁能帮助我找出如何将此XML编码为正确的格式? Thanks for your help! 谢谢你的帮助!

I would use iconv You can read about it in the manual here: http://php.net/manual/en/book.iconv.php 我会使用iconv您可以在以下手册中阅读它: http : //php.net/manual/zh/book.iconv.php

If you know for sure that it's UTF-8 initially, you can run this function: http://www.php.net/manual/en/function.iconv.php 如果您确定最初知道它是UTF-8,则可以运行以下功能: http : //www.php.net/manual/zh/function.iconv.php

iconv("UTF-8", "ISO-8859-1", $xml)

And send this to the API. 并将其发送到API。 It's strange the API would produce UTF-8, but require ISO-8859-1. API会产生UTF-8,但需要ISO-8859-1,这很奇怪。 Maybe I'm misunderstanding something though. 也许我误会了一些东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM