×
  • remind me tomorrow
  • remind me next week
  • never remind me
Subscribe to the ANN Newsletter • Wake up every Sunday to a curated list of ANN's most interesting posts of the week. read more

Forum - View topic
XML Response with PHP




Anime News Network Forum Index -> Site-related -> Encyclopedia
View previous topic :: View next topic  
Author Message
jrock20041



Joined: 21 Jul 2016
Posts: 8
Location: PA
PostPosted: Thu Jul 21, 2016 3:11 pm Reply with quote
So I am working on a page that takes in an anime title and you can get info on that title. I got curl setup and it seems to be returning me an xml object. But from here I cannot figure how to parse it. Can someone help?


Code:

<?php
    $curl = curl_init();

    curl_setopt_array($curl, array(
   CURLOPT_URL => "http://cdn.animenewsnetwork.com/encyclopedia/api.xml?title=~One%20Piece",
   CURLOPT_RETURNTRANSFER => true,
   CURLOPT_ENCODING => "",
   CURLOPT_CUSTOMREQUEST => "GET",
   CURLOPT_HTTPHEADER => array(
      "content-type: text/xml"
        )
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
   echo "cURL Error #:" . $err;
    } else {
   #$xml = str_replace(array("\n", "\r", "\t"), '', $response);
   #$xml = trim(str_replace('"', "'", $xml));

   $xml = json_encode($repsonse);
   $xml = str_replace(array("\n", "\r", "\t"), '', $xml);
   $xml = trim(str_replace('"', "'", $xml));

   $resp = new SimpleXMLElement($xml);

   echo $resp;
    }
?>

Back to top
View user's profile Send private message Visit poster's website
Shiroi Hane
Encyclopedia Editor


Joined: 25 Oct 2003
Posts: 7578
Location: Wales
PostPosted: Fri Jul 22, 2016 6:23 am Reply with quote
If you visit cdn.animenewsnetwork.com/encyclopedia/api.xml?title=~One%20Piece you can see the XML itself.
I think I did once write some code for parsing XML but that was a long time ago and I can't even find it for reference. I almost certainly adapted code from the notes on php.net however, since that's what I usually did. This seems like the place to start?
http://www.w3schools.com/php/php_xml_simplexml_read.asp
http://php.net/manual/en/simplexml.examples-basic.php
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number My Anime My Manga
jrock20041



Joined: 21 Jul 2016
Posts: 8
Location: PA
PostPosted: Fri Jul 22, 2016 8:12 am Reply with quote
Well I got it to work somewhat. It parses it now but its just putting everything into arrays and I am loosing the xml attributes. I tried doing this conversion in code pen with JS and it works a hell of alot better
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Reply to topic    Anime News Network Forum Index -> Site-related -> Encyclopedia All times are GMT - 5 Hours
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group