SchweppesAle Posted September 1, 2009 Share Posted September 1, 2009 hi, I'm trying to parse an xml string in the following format $response = " <system> <status>success</status> <resultlist> <resultlist id='1'> <result id='{field_name}'>{field_value}</result> ... </resultlist> <resultlist id='2'> <result id='{field_name}'>{field_value}</result> ... </resultlist> ... </resultlist> </system>"; I've tried running $xml[] = simplexml_load_string($response); //each resulting xml foreach($xml as $entry) { $result[] = $entry->resultlist->result[4]; } The result array returns NULL. Quote Link to comment https://forums.phpfreaks.com/topic/172699-solved-simplexml_load_string/ Share on other sites More sharing options...
ignace Posted September 1, 2009 Share Posted September 1, 2009 You are missing the xml document definition: <?xml version='1.0'?> Quote Link to comment https://forums.phpfreaks.com/topic/172699-solved-simplexml_load_string/#findComment-910314 Share on other sites More sharing options...
SchweppesAle Posted September 1, 2009 Author Share Posted September 1, 2009 I don't think they include that within the return string. http://www.streamsend.com/kb/idx/78/144/Subscribers/article/Search.html Quote Link to comment https://forums.phpfreaks.com/topic/172699-solved-simplexml_load_string/#findComment-910356 Share on other sites More sharing options...
JonnoTheDev Posted September 1, 2009 Share Posted September 1, 2009 The function returns an object. Why store in an array? $xml = simplexml_load_string($response); // debug object /* print "<pre>"; print_r($xml); print "</pre>"; exit(); */ foreach($xml->system as $child) { } Quote Link to comment https://forums.phpfreaks.com/topic/172699-solved-simplexml_load_string/#findComment-910364 Share on other sites More sharing options...
SchweppesAle Posted September 21, 2009 Author Share Posted September 21, 2009 ok, I'm no longer storing it within an array since it wasn't necessary. retrieves the return xml string public function list_users($config) { $login = $config->get_username(); $password = $config->get_password(); $listid = $config->get_listid(); $request = "<system>". "<action>getsubscribers</action>". "<authorization>". "<username>".$login."</username>". "<password>".$password."</password>". "</authorization>". "<parameterlist>". "<parameter id='List ID'><value>".$listid."</value></parameter>". "<parameter id='Search Relation'><value>and</value></parameter>". "<parameterarr>". "</parameterarr>". "</parameterlist>". "</system>"; return $this->_xml = $request; } public function query($entry) { // Configuration $user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"; $url = "http://server1.streamsend.com/streamsend/api.php"; // Build Parameters $params['xmldata'] = $entry; // Open Connection Handle $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_POST,1); curl_setopt($ch, CURLOPT_POSTFIELDS,$params); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); // Send Request $response = curl_exec ($ch); // Close Connection Handle curl_close ($ch); $xml = simplexml_load_string($response); return $this->_result = $xml; } bit of code which should be parsing the xml string //$list == the resulting xml from the above code foreach($list->resultlist as $item) { echo '<pre>'; echo var_dump($item); echo '</pre>'; } This returns every user who met the search criteria How would I reference each one of them uniquely though? You'll see from reading the streamsend API documentation that the string will return a number of <result> tags; each one made unique by an "id" property value. foreach($list->resultlist->result as $item) still returns a blank page. Maybe I need to reference each of the result tags based on property value? Quote Link to comment https://forums.phpfreaks.com/topic/172699-solved-simplexml_load_string/#findComment-922495 Share on other sites More sharing options...
SchweppesAle Posted September 21, 2009 Author Share Posted September 21, 2009 doh...alright nevermind. This solved the problem foreach($list->resultlist->resultlist->result as $item) { //do something } Quote Link to comment https://forums.phpfreaks.com/topic/172699-solved-simplexml_load_string/#findComment-922500 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.