Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Sep 2008
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Formatting text to XML?

    Ok,

    So I am very new to PHP. My two partners and I have been working on a mashup that takes data from political websites and maps certain data to a google map. Because of the same origin policy we couldn't use Javascript to do the GET requests we needed; so we resorted to PHP. Using some PHP and cURL, we were able to get the text returned from the different API requests. The text is visually formatted in XML, but it is getting returned from cURL as plain text. I was wondering if there was a way to be able to return XML. Essentially, we are using a javascript function to call a php file that does the requests.

    Please let me know if this doesn't make any sense. Thanks yall.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Thats no problem. Use either the DOM or a simplexml to convert it into actual usable xml in php. I'm a fan of DOM myself, so that would be constructed as simple as:
    PHP Code:
    $xml DOMDocument::loadXML($yourCURLResponse); 
    And simple xml is as simple as:
    PHP Code:
    $xml = new SimpleXML($yourCURLResponse); 
    Note that if this is simply to push back to JS, than you needn't at all push this into a XML type. Simply add a header:
    PHP Code:
    header('Content-type: text/xml'); 
    before any output, and print the results of the curl response. That will now be served as an XML document instead of a text/plain. Only use dom/simplexml if you plan on actually manipulating the xml in some fashion.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by jbm715 View Post
    Ok,

    So I am very new to PHP. My two partners and I have been working on a mashup that takes data from political websites and maps certain data to a google map. Because of the same origin policy we couldn't use Javascript to do the GET requests we needed; so we resorted to PHP. Using some PHP and cURL, we were able to get the text returned from the different API requests. The text is visually formatted in XML, but it is getting returned from cURL as plain text. I was wondering if there was a way to be able to return XML. Essentially, we are using a javascript function to call a php file that does the requests.

    Please let me know if this doesn't make any sense. Thanks yall.
    http://opengovernmentdata.org/data/map/

    best regards

  • #4
    New to the CF scene
    Join Date
    Sep 2008
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Nice website but i'm not asking a question about goverment mashups. I guess I should've mentioned the project is academic. Any idea about my xml question?

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by jbm715 View Post
    Nice website but i'm not asking a question about goverment mashups. I guess I should've mentioned the project is academic. Any idea about my xml question?
    yes, kml is xml and you don't need to scrap pages for data because there are already started many projects for open gov data in few country. If you are from europe take a look to ckan.

    http://ckan.net/

    you also can find some tools which work with rdf/xml.
    Shortest story the site is only a starting point to a not so new technology,
    unfortunately there are only few projects which use php and i don't think this will help you too much.

    best regards
    Last edited by oesxyl; 04-30-2011 at 08:19 AM.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •