如何使用PHP将JSON中的Instagram API转换为XML?

时间:2014-01-09 10:02:34

标签: php xml json

我目前正在努力将JSON从Instagram API转换为XML,以便我可以将其融入我的网站。 API URI示例:https://api.instagram.com/v1/users/3/media/recent/?access_token=180213154.5d9f31c.67ff117fa05643aab53e209dec05eb01&count=1 结果:

{"pagination":{"next_url":"https:\/\/api.instagram.com\/v1\/users\/3\/media\/recent?access_token=180213154.5d9f31c.67ff117fa05643aab53e209dec05eb01\u0026count=1\u0026max_id=627998676974834410_3","next_max_id":"627998676974834410_3"},"meta":{"code":200},"data":[{"attribution":null,"tags":[],"type":"image","location":{"latitude":35.66343355,"name":"STREAMER COFFEE COMPANY Shibuya","longitude":139.703916256,"id":85987},"comments":{"count":253,"data":[{"created_time":"1389227943","text":"Found a massive flaw in your security settings regarding blocking users. How can I get in contact with someone about it? It's not covered in the help centre.","from":{"username":"sorehands","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_373015_75sq_1388722624.jpg","id":"373015","full_name":"Jim Madden"},"id":"629212055559213459"},{"created_time":"1389229366","text":"Can i ask why my ins account only can follow 600 people???","from":{"username":"ginameng9208","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_411563068_75sq_1389151537.jpg","id":"411563068","full_name":"Gina"},"id":"629223987146133573"},{"created_time":"1389229770","text":"Green Hornettt","from":{"username":"christian_anthony64","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_602175818_75sq_1388897977.jpg","id":"602175818","full_name":"Christian Anthony Hynick"},"id":"629227377116844283"},{"created_time":"1389232974","text":"Do you enjoy people asking you random questions with no connection to the photo you posted? @kevin @garrett","from":{"username":"jimbo","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_73292_75sq_1329450021.jpg","id":"73292","full_name":"James Mellors"},"id":"629254258520232796"},{"created_time":"1389234525","text":"Delete blah420hhhh ASAP","from":{"username":"mariaglam27","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_22333079_75sq_1388097173.jpg","id":"22333079","full_name":"\ud83c\udf1fMARIA\ud83c\udf1f"},"id":"629267267019870954"},{"created_time":"1389244448","text":"It's like ferns","from":{"username":"jellybaby304x","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_875122748_75sq_1388550325.jpg","id":"875122748","full_name":"Jaime"},"id":"629350503418958807"},{"created_time":"1389246207","text":"How did you do that? That's so awesome.","from":{"username":"cutepets5698","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/anonymousUser.jpg","id":"905777437","full_name":"cutepets5698"},"id":"629365258560999971"},{"created_time":"1389249463","text":"Thank you for creating an amazing platform for me to produce and consume raw media!","from":{"username":"goldaviators","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_14617395_75sq_1388635603.jpg","id":"14617395","full_name":"Shelton"},"id":"629392576180392401"}]},"filter":"Valencia","created_time":"1389083297","link":"http:\/\/instagram.com\/p\/i3GXgsgBLq\/","likes":{"count":15972,"data":[{"username":"tylermccormack16","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_630160402_75sq_1386021156.jpg","id":"630160402","full_name":"Tyler Mccormack"},{"username":"badetcor","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_493933756_75sq_1375603183.jpg","id":"493933756","full_name":"badetcor"},{"username":"cherrygandararivera","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/anonymousUser.jpg","id":"924993914","full_name":"cherrygandararivera@yahoo.com"},{"username":"_noah2003","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_637433886_75sq_1383646066.jpg","id":"637433886","full_name":"FOLLOW ME BITCH!!!!!!"}]},"images":{"low_resolution":{"url":"http:\/\/distilleryimage2.s3.amazonaws.com\/8db13ee2777511e3a801121e0f202bd6_6.jpg","width":306,"height":306},"thumbnail":{"url":"http:\/\/distilleryimage2.s3.amazonaws.com\/8db13ee2777511e3a801121e0f202bd6_5.jpg","width":150,"height":150},"standard_resolution":{"url":"http:\/\/distilleryimage2.s3.amazonaws.com\/8db13ee2777511e3a801121e0f202bd6_8.jpg","width":640,"height":640}},"users_in_photo":[],"caption":{"created_time":"1389083297","text":"Still dreaming of the latte art in Tokyo \ud83c\uddef\ud83c\uddf5","from":{"username":"kevin","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_3_75sq_1325536697.jpg","id":"3","full_name":"Kevin Systrom"},"id":"627998677444596365"},"user_has_liked":false,"id":"627998676974834410_3","user":{"username":"kevin","website":"","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_3_75sq_1325536697.jpg","full_name":"Kevin Systrom","bio":"","id":"3"}}]}

如何使用PHP将其转换为XML,如下所示。

<products>
<category categoryName="Electronics">
    <product productName="Camera Lenses Collection" productID="DSCF0001" thumbPath="thumbs/Electronics/camera_lenses_collection-other.jpg" productPrice="250.50">
        <details>
        <![CDATA[
        <p class="four">You can place your HTML CSS formatted content</p>
    <p><strong>Lorem Ipsum</strong> is simply dummy text of the printing and  typesetting industry. <span class="three">Lorem Ipsum has been the industry's standard </span>dummy text ever since the 1500s, when an unknown printer took a galley  of type and scrambled it to make a type specimen book. It has survived  not only five centuries, but also the leap into electronic typesetting,  remaining essentially unchanged. It was popularised in the 1960s with  the release of Letraset sheets containing Lorem Ipsum passages, and  more recently with desktop publishing software like Aldus PageMaker  including versions of Lorem Ipsum.</p>
    <p><img src="images/Electronics/camera_lenses_collection-other.jpg" /><strong>Lorem Ipsum</strong> is simply dummy text of the printing and  typesetting industry. <span class="three">Lorem Ipsum has been the industry's standard </span>dummy text ever since the 1500s, when an unknown printer took a galley  of type and scrambled it to make a type specimen book. It has survived  not only five centuries, but also the leap into electronic typesetting,  remaining essentially unchanged. It was popularised in the 1960s with  the release of Letraset sheets containing Lorem Ipsum passages, and  more recently with desktop publishing software like Aldus PageMaker  including versions of Lorem Ipsum.</p> 
    ]]></details>
</product>
</category>

1 个答案:

答案 0 :(得分:1)

试试这个XML Serializer。你可以在这里看到另一个问题,其中一些示例答案可能有所帮助Is there some way to convert json to xml in PHP?同样,您可以使用json_decode。