Forum Moderators: coopster

Message Too Old, No Replies

Posting XML data and getting XML data back

         

eatspinach

12:44 pm on Mar 15, 2010 (gmt 0)

10+ Year Member



Hey,

I need to make a curl request to a URL and retrieve XML data back, the trouble i am having is that the response that is coming back is not XML.

this is my code, does anyone know what i am doing wrong?

function sendData($xmlData)
{
$curlObject = curl_init();
//Sets an option on the given cURL session handle
curl_setopt($curlObject, CURLOPT_URL, "http://myurl.com");
//do not get the Header
curl_setopt ($curlObject, CURLOPT_HEADER, false);
// get the response as a string from curl_exec(), rather than echoing it
curl_setopt ($curlObject, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($curlObject, CURLOPT_POST, true);
curl_setopt ($curlObject, CURLOPT_POSTFIELDS, "$xmlData");
$response = curl_exec($curlObject);

curl_close ($curlObject);

return $response;
}



Thanks for any help in advance.

Anyango

2:40 pm on Mar 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



But won't that depend on the url which is sending u response back ? it has to be sent as XML from there. your code looks fine

eatspinach

2:51 pm on Mar 15, 2010 (gmt 0)

10+ Year Member



I found some nice code that converted my response into xml data which is getting me half of the way there...

$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser,XML_OPTION_CASE_FOLDING,0);
xml_parser_set_option($xml_parser,XML_OPTION_SKIP_WHITE,1);
xml_parse_into_struct($xml_parser, $data, $vals, $index);
xml_parser_free($xml_parser);


so now i need to find some code that will go through the xml nodes and get the information i need, does anyone know of any good resources? sorry this is the first time i worked with xml

eatspinach

4:52 pm on Mar 15, 2010 (gmt 0)

10+ Year Member



To explain my problem a little better, when i run this: print_r($vals);

i get


Array ( [0] => Array ( [tag] => xml [type] => open [level] => 1 ) [1] => Array ( [tag] => command [type] => complete [level] => 2 [attributes] => Array ( [name] => find [result] => 0 ) ) [2] => Array ( [tag] => Campaign [type] => open [level] => 2 ) [3] => Array ( [tag] => Id [type] => complete [level] => 3 [value] => 88 ) [4] => Array ( [tag] => Name [type] => complete [level] => 3 [value] => dave test ) [5] => Array ( [tag] => Campaign [type] => close [level] => 2 ) [6] => Array ( [tag] => Campaign [type] => open [level] => 2 ) [7] => Array ( [tag] => Id [type] => complete [level] => 3 [value] => 87 ) [8] => Array ( [tag] => Name [type] => complete [level] => 3 [value] => campaign-6 ) [9] => Array ( [tag] => Campaign [type] => close [level] => 2 ) [10] => Array ( [tag] => Campaign [type] => open [level] => 2 ) [11] => Array ( [tag] => Id [type] => complete [level] => 3 [value] => 86 ) [12] => Array ( [tag] => Name [type] => complete [level] => 3 [value] => campaign-6 ) [13] => Array ( [tag] => Campaign [type] => close [level] => 2 ) [14] => Array ( [tag] => Campaign [type] => open [level] => 2 ) [15] => Array ( [tag] => Id [type] => complete [level] => 3 [value] => 85 ) [16] => Array ( [tag] => Name [type] => complete [level] => 3 [value] => campaign-6 ) [17] => Array ( [tag] => Campaign [type] => close [level] => 2 ) [18] => Array ( [tag] => Campaign [type] => open [level] => 2 ) [19] => Array ( [tag] => Id [type] => complete [level] => 3 [value] => 54 ) [20] => Array ( [tag] => Name [type] => complete [level] => 3 [value] => campaign-2 ) [21] => Array ( [tag] => Campaign [type] => close [level] => 2 ) [22] => Array ( [tag] => Campaign [type] => open [level] => 2 ) [23] => Array ( [tag] => Id [type] => complete [level] => 3 [value] => 83 ) [24] => Array ( [tag] => Name [type] => complete [level] => 3 [value] => campaign-5 ) [25] => Array ( [tag] => Campaign [type] => close [level] => 2 ) [26] => Array ( [tag] => Campaign [type] => open [level] => 2 ) [27] => Array ( [tag] => Id [type] => complete [level] => 3 [value] => 82 ) [28] => Array ( [tag] => Name [type] => complete [level] => 3 [value] => campaign-4 ) [29] => Array ( [tag] => Campaign [type] => close [level] => 2 ) [30] => Array ( [tag] => Campaign [type] => open [level] => 2 ) [31] => Array ( [tag] => Id [type] => complete [level] => 3 [value] => 81 ) [32] => Array ( [tag] => Name [type] => complete [level] => 3 [value] => campaign-3 ) [33] => Array ( [tag] => Campaign [type] => close [level] => 2 ) [34] => Array ( [tag] => Campaign [type] => open [level] => 2 ) [35] => Array ( [tag] => Id [type] => complete [level] => 3 [value] => 50 ) [36] => Array ( [tag] => Name [type] => complete [level] => 3 [value] => campaign-1 ) [37] => Array ( [tag] => Campaign [type] => close [level] => 2 ) [38] => Array ( [tag] => xml [type] => close [level] => 1 ) )


It should look like this,

<xml><command name="find" result="0"/>

<Campaign><Id>59</Id><Name>remote3</Name></Campaign>

<Campaign><Id>61</Id><Name>remote5</Name></Campaign>

<Campaign><Id>58</Id><Name>remote2</Name></Campaign>

<Campaign><Id>60</Id><Name>remote4</Name></Campaign>

<Campaign><Id>57</Id><Name>remote1</Name></Campaign>

<Campaign><Id>70</Id><Name>remote04</Name></Campaign>

<Campaign><Id>68</Id><Name>remote02</Name></Campaign>

<Campaign><Id>67</Id><Name>remote01</Name></Campaign>

<Campaign><Id>65</Id><Name>remote8</Name></Campaign>

<Campaign><Id>64</Id><Name>remote7</Name></Campaign>

</xml>



I just need to get something that will loop through the response and then i can output the campaign id and campaign name into a html table.

Anyango

5:15 pm on Mar 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are many xml2Array functions available to start with, search for xml to array. send your $data to it and it will convert it into array for you then you can loop through it.

eatspinach

10:13 am on Mar 16, 2010 (gmt 0)

10+ Year Member



I tried this xmltoarray function.

function xml2array($xml) {
$xmlary = array();

$reels = '/<(\w+)\s*([^\/>]*)\s*(?:\/>|>(.*)<\/\s*\\1\s*>)/s';
$reattrs = '/(\w+)=(?:"|\')([^"\']*)(:?"|\')/';

preg_match_all($reels, $xml, $elements);

foreach ($elements[1] as $ie => $xx) {
$xmlary[$ie]["name"] = $elements[1][$ie];

if ($attributes = trim($elements[2][$ie])) {
preg_match_all($reattrs, $attributes, $att);
foreach ($att[1] as $ia => $xx)
$xmlary[$ie]["attributes"][$att[1][$ia]] = $att[2][$ia];
}

$cdend = strpos($elements[3][$ie], "<");
if ($cdend > 0) {
$xmlary[$ie]["text"] = substr($elements[3][$ie], 0, $cdend - 1);
}

if (preg_match($reels, $elements[3][$ie]))
$xmlary[$ie]["elements"] = xml2array($elements[3][$ie]);
else if ($elements[3][$ie]) {
$xmlary[$ie]["text"] = $elements[3][$ie];
}
}

return $xmlary;
}


and i send the data in like so

$xml=xml2array(file_get_contents($data));
print_r($xml);


I am getting a warning messages which shows the information the way it should be ie

Warning: file_get_contents(<xml><command name="find" result="0"/> <Campaign><Id>88</Id><Name>dave test</Name></Campaign> <Campaign><Id>87</Id><Name>campaign-6</Name></Campaign> <Campaign><Id>86</Id><Name>campaign-6</Name></Campaign> <Campaign><Id>85</Id><Name>campaign-6</Name></Campaign> <Campaign><Id>54</Id><Name>campaign-2</Name></Campaign> <Campaign><Id>83</Id><Name>campaign-5</Name></Campaign> <Campaign><Id>82</Id><Name>campaign-4</Name></Campaign> <Campaign><Id>81</Id><Name>campaign-3</Name></Campaign> <Campaign><Id>50</Id><Name>campaign-1</Name></Campaign> </xml> ) [function.file-get-contents]: failed to o in C:\xampp\htdocs\sms\fns\curl_fns.php on line 49


so i changed the line of code from

$xml=xml2array(file_get_contents($data));


to

$xml=xml2array($data);


because the information is not coming from a file its coming from a string of data in the code above.

and then i get an array but some of the values are missing.

Array ( [0] => Array ( [name] => xml [elements] => Array ( [0] => Array ( [name] => command [attributes] => Array ( [name] => find [result] => 0 ) ) [1] => Array ( [name] => Campaign [elements] => Array ( [0] => Array ( [name] => Id [text] => 8 [elements] => Array ( [0] => Array ( [name] => Name [text] => dave tes [elements] => Array ( [0] => Array ( [name] => Campaign [elements] => Array ( [0] => Array ( [name] => Id [text] => 8 [elements] => Array ( [0] => Array ( [name] => Name [text] => campaign- [elements] => Array ( [0] => Array ( [name] => Campaign [elements] => Array ( [0] => Array ( [name] => Id [text] => 8 [elements] => Array ( [0] => Array ( [name] => Name [text] => campaign- [elements] => Array ( [0] => Array ( [name] => Id [text] => 85 ) ) ) ) ) [1] => Array ( [name] => Name [text] => campaign-2 ) ) ) [1] => Array ( [name] => Id [text] => 83 ) ) ) ) ) [1] => Array ( [name] => Name [text] => campaign-4 ) ) ) [1] => Array ( [name] => Id [text] => 81 ) ) ) ) ) [1] => Array ( [name] => Name [text] => campaign-1 ) ) ) ) ) ) 


campaign-5 and campaign-6 are now missing, so i must be doing something wrong. any idea?

Sorry for the lengthy post :)

eatspinach

10:52 am on Mar 16, 2010 (gmt 0)

10+ Year Member



To briefly summarise the problem.

I have some xml data from a cUrl response that looks like this,

<xml><command name="find" result="0"/> 

<Campaign><Id>88</Id><Name>dave test</Name></Campaign>
<Campaign><Id>87</Id><Name>campaign-6</Name></Campaign>
<Campaign><Id>86</Id><Name>campaign-6</Name></Campaign>
<Campaign><Id>85</Id><Name>campaign-6</Name></Campaign>
<Campaign><Id>54</Id><Name>campaign-2</Name></Campaign>
<Campaign><Id>83</Id><Name>campaign-5</Name></Campaign>
<Campaign><Id>82</Id><Name>campaign-4</Name></Campaign>
<Campaign><Id>81</Id><Name>campaign-3</Name></Campaign>
<Campaign><Id>50</Id><Name>campaign-1</Name></Campaign>
</xml>


and i need to get it to look like this..



<table border="0" cellspacing="0">
<tr>
<td></td>
<td>Campaign Id</td>
<td>Campaign Name</td>
</tr>
<tr>
<td></td>
<td>88</td>
<td>dave test</td>
</tr>
<tr>
<td></td>
<td>87</td>
<td>campaign-6</td>
</tr>
<tr>
<td></td>
<td>86</td>
<td>campaign-6</td>
</tr>

..etc

</table>


xml is definitely not my strong point :)

eatspinach

12:10 pm on Mar 16, 2010 (gmt 0)

10+ Year Member



problem solved: i just used a different xml2arraymethod.. i used this one

[bin-co.com...]

Anyango

6:50 pm on Mar 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



glad you figured it out :)