Crew-Portal Posted December 11, 2007 Share Posted December 11, 2007 $xml = $xmlparse->GetXMLTree('http://24.76.166.219:81/xml'); This is the script I am using to make a XML conection on a server that I operate. The default time for the connection is 60 seconds. As is if the server doesnt respond within 60 seconds the connection dies and I get an error message. But how do I change the Connection time allowed to 20 seconds and as the server attempts to connect I get a message like "Conecting To 24.76.166.219/xml" and once connection is sucessful it refreshes to another page with data from the XML You kinda get what I mean? Quote Link to comment Share on other sites More sharing options...
JacobYaYa Posted December 11, 2007 Share Posted December 11, 2007 ini_set('default_socket_timeout', $seconds); use that and file_get_contents() to fill a variable that you will pass to GetXMLTree() Some good examples here http://www.php.net/manual/en/function.stream-set-timeout.php Quote Link to comment Share on other sites More sharing options...
Crew-Portal Posted December 11, 2007 Author Share Posted December 11, 2007 Oh no i dont use file get contents its puts the data into an array for me. So lets say the XML looks like: <1> <2> <3> This Value 3 </3> </2> </1> The script calls it like $xml['1']['0']['2']['0']['3']['0']['value'] But yes the timeout thing works great thank you! Quote Link to comment Share on other sites More sharing options...
JacobYaYa Posted December 11, 2007 Share Posted December 11, 2007 Didn't explain that very well but I'm not 100% sure that stream-set-timeout() works with file_get_contents() but I know it works with fopen(). I know that init_set() works with file_get_contents(). The other option you have is to use curl() and curl_setopt () with 'CURLOPT_CONNECTTIMEOUT'. Quote Link to comment Share on other sites More sharing options...
JacobYaYa Posted December 11, 2007 Share Posted December 11, 2007 Oh no i dont use file get contents its puts the data into an array for me. So lets say the XML looks like: <1> <2> <3> This Value 3 </3> </2> </1> The script calls it like $xml['1']['0']['2']['0']['3']['0']['value'] But yes the timeout thing works great thank you! Cool. Quote Link to comment Share on other sites More sharing options...
Crew-Portal Posted December 11, 2007 Author Share Posted December 11, 2007 To Give you the code because you seem interested: <?php class ParseXML{ function GetChildren($vals, &$i) { $children = array(); // Contains node data if (isset($vals[$i]['value'])){ $children['VALUE'] = $vals[$i]['value']; } while (++$i < count($vals)){ switch ($vals[$i]['type']){ case 'cdata': if (isset($children['VALUE'])){ $children['VALUE'] .= $vals[$i]['value']; } else { $children['VALUE'] = $vals[$i]['value']; } break; case 'complete': if (isset($vals[$i]['attributes'])) { $children[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes']; $index = count($children[$vals[$i]['tag']])-1; if (isset($vals[$i]['value'])){ $children[$vals[$i]['tag']][$index]['VALUE'] = $vals[$i]['value']; } else { $children[$vals[$i]['tag']][$index]['VALUE'] = ''; } } else { if (isset($vals[$i]['value'])){ $children[$vals[$i]['tag']][]['VALUE'] = $vals[$i]['value']; } else { $children[$vals[$i]['tag']][]['VALUE'] = ''; } } break; case 'open': if (isset($vals[$i]['attributes'])) { $children[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes']; $index = count($children[$vals[$i]['tag']])-1; $children[$vals[$i]['tag']][$index] = array_merge($children[$vals[$i]['tag']][$index],$this->GetChildren($vals, $i)); } else { $children[$vals[$i]['tag']][] = $this->GetChildren($vals, $i); } break; case 'close': return $children; } } } function GetXMLTree($xmlloc){ if (file_exists($xmlloc)){ $data = implode('', file($xmlloc)); } else { $fp = fopen($xmlloc,'r'); while(!feof($fp)){ $data = $data . fread($fp, 1024); } fclose($fp); } $parser = xml_parser_create('ISO-8859-1'); xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); xml_parse_into_struct($parser, $data, $vals, $index); xml_parser_free($parser); $tree = array(); $i = 0; if (isset($vals[$i]['attributes'])) { $tree[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes']; $index = count($tree[$vals[$i]['tag']])-1; $tree[$vals[$i]['tag']][$index] = array_merge($tree[$vals[$i]['tag']][$index], $this->GetChildren($vals, $i)); } else { $tree[$vals[$i]['tag']][] = $this->GetChildren($vals, $i); } return $tree; } } $xmlparse = &new ParseXML; $xml = $xmlparse->GetXMLTree('http://24.76.166.219:81/xml'); ?> Quote Link to comment 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.