Cobra23 Posted January 25, 2012 Share Posted January 25, 2012 Hello, I am using curl with it's code like this: <?php $urls = 'http://www.a-url.com'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $urls); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, "xml=".urlencode($xml_posts)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close')); $result=curl_exec($ch); var_dump($result); $data = XML_unserialize($result); echo ($data); ?> and using the XML Parse by: # XML Library, by Keith Devens, version 1.2b # http://keithdevens.com/software/phpxml When i echo $data and $result i can see them on the front end and the xml in the source code once i view it. How do i add a SimpleXML to this as i want to get some of the values from the xml file displayed in a array with a number of rows. Does this mean getting rid of the XMLParser function file and using a SimpleXML function file. Can i not use both? When i try to use the SimpleXML functions, i'm clueless in how to get it's data from the xml $results. The XML is the source can be shown like this with me only showing one line: <Result> <Info> <Details> <Type> <Who> <Name key="1">This is Me</Name> <Number1 key="2">18.93</Number1> <Number2 key="2">20.01</Number2> <Number3 key="2">15.21</Number3> </Who> </Type> </Details> </Info> </Result> Can somebody please help me with this ? I need to getthis sorted !! Quote Link to comment https://forums.phpfreaks.com/topic/255759-using-curl-and-simplexml-together/ Share on other sites More sharing options...
Cobra23 Posted January 25, 2012 Author Share Posted January 25, 2012 Or even an opinion ? by the way, i'm using: <?php print_r ($data); ?> instead of what i have displayed above: <?php echo ($data); ?> Quote Link to comment https://forums.phpfreaks.com/topic/255759-using-curl-and-simplexml-together/#findComment-1311093 Share on other sites More sharing options...
codebyren Posted January 25, 2012 Share Posted January 25, 2012 I'd say learning to use SimpleXML is quite valuable long-term but there's nothing stopping you from using it alongside you current library of choice. With your sample XML: <?php $xml = '<Result><Info><Details><Type><Who><Name key="1">This is Me</Name><Number1 key="2">18.93</Number1><Number2 key="2">20.01</Number2><Number3 key="2">15.21</Number3></Who></Type></Details></Info></Result>'; $xml = simplexml_load_string($xml); // This is not ideal for seeing attributes but gives you a good idea of the XML tree/structure // print_r($xml); // Access the first <Name> in the XML echo "The first instance of name is: ", $xml->Info->Details->Type->Who->Name[0], "<br>"; // (notice how Info->Details->Type->Who->Name matches the XML tree/structure making it pretty easy to navigate) // Access the attributes tied to the first instance of <Name> using atributes() method foreach($xml->Info->Details->Type->Who->Name[0]->attributes() as $key => $val) { echo "The key is $key and the value is $val. <br>"; } exit; ?> Hope this gets you going in the right direction... Quote Link to comment https://forums.phpfreaks.com/topic/255759-using-curl-and-simplexml-together/#findComment-1311179 Share on other sites More sharing options...
Cobra23 Posted February 2, 2012 Author Share Posted February 2, 2012 Hi Thanks, It helped alot. Another way to do it is shown below and use a for or while loop with the $i: <?php $rowresult = $data[Result][info][Details][Type][$i][Who] ?> This would display the rows for $rowresult. Now as it's coming from a 3rd party server i had to write in some php codes to cancel out certain data that was set thru a submit form. However, as i want each row to be displaying a different background color, i need to get the number of row's from the new results. I tried to use "count" but it gives me the result like: 1 1 1 1 1 1 1 1 How can i sort that? Quote Link to comment https://forums.phpfreaks.com/topic/255759-using-curl-and-simplexml-together/#findComment-1313542 Share on other sites More sharing options...
Cobra23 Posted February 2, 2012 Author Share Posted February 2, 2012 I mean, how do i sort a "count" that displays: 8 as in the total counts, instead of: 1 1 1 1 1 1 1 1 Quote Link to comment https://forums.phpfreaks.com/topic/255759-using-curl-and-simplexml-together/#findComment-1313551 Share on other sites More sharing options...
Cobra23 Posted February 2, 2012 Author Share Posted February 2, 2012 ooops, sorry guys. The code should actually be according to that xml: <?php $rowresult = $data[Result][info][Details][Type][Who][$i][Name]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/255759-using-curl-and-simplexml-together/#findComment-1313559 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.