Jump to content

Using Curl and SimpleXML together ??


Cobra23

Recommended Posts

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 !!

Link to comment
https://forums.phpfreaks.com/topic/255759-using-curl-and-simplexml-together/
Share on other sites

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...

 

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.