Jump to content

monkeypaw201

Members
  • Posts

    376
  • Joined

  • Last visited

Posts posted by monkeypaw201

  1. There is a poorly designed API library that returns data either as a PHP array or XML and to keep things standardized within my application I wanted to check if the return was XML and if so, convert to an array so the library is always outputting the same format.

  2. Hello,

     

    I have a function that converts xml2array, however it doesn't retain CDATA (multi-lines become one line). I'm not quite sure what I need to change in order to make it retain the CDATA (perhaps convert it to htmlentities).

     

    Any ideas?

    	function xml2array($contents, $get_attributes=1, $priority = 'tag') { 
    		if(!$contents) return array(); 
    	
    		if(!function_exists('xml_parser_create')) { 
    			//print "'xml_parser_create()' function not found!"; 
    			return array(); 
    		} 
    	
    		//Get the XML parser of PHP - PHP must have this module for the parser to work 
    		$parser = xml_parser_create(''); 
    		xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
    		xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); 
    		xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); 
    		xml_parse_into_struct($parser, trim($contents), $xml_values); 
    		xml_parser_free($parser); 
    	
    		if(!$xml_values) return;//Hmm... 
    	
    		//Initializations 
    		$xml_array = array(); 
    		$parents = array(); 
    		$opened_tags = array(); 
    		$arr = array(); 
    	
    		$current = &$xml_array; //Refference 
    	
    		//Go through the tags. 
    		$repeated_tag_index = array();//Multiple tags with same name will be turned into an array 
    		foreach($xml_values as $data) { 
    			unset($attributes,$value);//Remove existing values, or there will be trouble 
    	
    			//This command will extract these variables into the foreach scope 
    			// tag(string), type(string), level(int), attributes(array). 
    			extract($data);//We could use the array by itself, but this cooler. 
    	
    			$result = array(); 
    			$attributes_data = array(); 
    			 
    			if(isset($value)) { 
    				if($priority == 'tag') $result = $value; 
    				else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode 
    			} 
    	
    			//Set the attributes too. 
    			if(isset($attributes) and $get_attributes) { 
    				foreach($attributes as $attr => $val) { 
    					if($priority == 'tag') $attributes_data[$attr] = $val; 
    					else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr' 
    				} 
    			} 
    	
    			//See tag status and do the needed. 
    			if($type == "open") {//The starting of the tag '<tag>' 
    				$parent[$level-1] = &$current; 
    				if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag 
    					$current[$tag] = $result; 
    					if($attributes_data) $current[$tag. '_attr'] = $attributes_data; 
    					$repeated_tag_index[$tag.'_'.$level] = 1; 
    	
    					$current = &$current[$tag]; 
    	
    				} else { //There was another element with the same tag name 
    	
    					if(isset($current[$tag][0])) {//If there is a 0th element it is already an array 
    						$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result; 
    						$repeated_tag_index[$tag.'_'.$level]++; 
    					} else {//This section will make the value an array if multiple tags with the same name appear together 
    						$current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array 
    						$repeated_tag_index[$tag.'_'.$level] = 2; 
    						 
    						if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well 
    							$current[$tag]['0_attr'] = $current[$tag.'_attr']; 
    							unset($current[$tag.'_attr']); 
    						} 
    	
    					} 
    					$last_item_index = $repeated_tag_index[$tag.'_'.$level]-1; 
    					$current = &$current[$tag][$last_item_index]; 
    				} 
    	
    			} elseif($type == "complete") { //Tags that ends in 1 line '<tag />' 
    				//See if the key is already taken. 
    				if(!isset($current[$tag])) { //New Key 
    					$current[$tag] = $result; 
    					$repeated_tag_index[$tag.'_'.$level] = 1; 
    					if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data; 
    	
    				} else { //If taken, put all things inside a list(array) 
    					if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array... 
    	
    						// ...push the new element into that array. 
    						$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result; 
    						 
    						if($priority == 'tag' and $get_attributes and $attributes_data) { 
    							$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data; 
    						} 
    						$repeated_tag_index[$tag.'_'.$level]++; 
    	
    					} else { //If it is not an array... 
    						$current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value 
    						$repeated_tag_index[$tag.'_'.$level] = 1; 
    						if($priority == 'tag' and $get_attributes) { 
    							if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well 
    								 
    								$current[$tag]['0_attr'] = $current[$tag.'_attr']; 
    								unset($current[$tag.'_attr']); 
    							} 
    							 
    							if($attributes_data) { 
    								$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data; 
    							} 
    						} 
    						$repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken 
    					} 
    				} 
    	
    			} elseif($type == 'close') { //End of tag '</tag>' 
    				$current = &$parent[$level-1]; 
    			} 
    		} 
    		 
    		return($xml_array); 
    	} 
    
    
  3.  

    The reason why it's bad is that queries should never, ever, ever be performed in a loop.  It's a surefire way to kill site performance, as it costs more to hit a database repeatedly with queries than it does one long query (generally speaking).

     

    Thank you for elaborating to assist others (like myself) by learning something new.

  4. Right off the bat, I see

     

     

    window.location.href = "/MainPageindexSearch.php?ItemValue=" + $("#searchvalue").val(); 
    

     

    and

     

    window.location.href = "/MainPageindexSearch.php?Value=" + $("#Newsearchvalue").val(); 
    

     

    Shouldn't these be the same? Otherwise you're looking for a value that doesn't exist?

  5. A csv file is simply a comma-delimited file with each entry being on its own line

     

    It's format is:

     

    column1,column2,column3,column4

    column1,column2,column3,column4

     

    If you have more complex strings, put quotes around some or all of the columns:

     

    colum1,"a complete sentence",order_number,something

    colum1,"a complete sentence",order_number,something

  6. I don't know if I see where the problem lies.

     

    The email address is retrieved from the database and then written into a variable. That variable is then used in the mail() function (or something equivalent). If the user cannot modify that variable they can't send it to multiple emails correct?

     

    If they try to append multiple numbers in the URL the database won't return a match (eg. it will match "123" but not "123,456").

     

    So as long as the number going in is clean (mysql escape?) it should be fine.

     

    Did I miss something?

  7. Hi jkkenzie,

     

    Looking at the source code, there appear to be some hidden input fields that are being sent. Primarily a "__VIEWSTATE", which I would assume is some sort of session/temporary key to try to prevent what you are trying to do.

     

    However, never fear! There is a solution :)

     

    Here is some revised code. I've tested it and instead of giving me a login box it gives me an internal server error. Try it with actual credentials and let us know what happens.

     

    <?php
    
    //initiate curl process
    $ch = curl_init();
    $url = "http://track.futuresystems.co.ke/";
    
    //set options
    /*
    There are a number of options, which you can use to manipulate
    the behaviour of this ''invisible browser''.
    See php.net/curl for more details.
    */
    
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/My Test Browser");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    
    //run the process and fetch the document
    $doc = curl_exec($ch);
    
    
    // extract viewstate input field
    $viewstate = explode('<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="',$doc);
    $viewstate = explode('" />',$viewstate[1]);
    $viewstate = $viewstate[0];
    
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/My Test Browser");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    // ENABLE HTTP POST
    curl_setopt ($ch, CURLOPT_POST, 1);
    
    // SET POST PARAMETERS : FORM VALUES FOR EACH FIELD
    curl_setopt ($ch, CURLOPT_POSTFIELDS, 'txtUserName3=******&txtPassword3=*****&__VIEWSTATE='.$viewstate);
    
    //run the process and fetch the document
    $doc = curl_exec($ch);
    
    //terminate curl process
    curl_close($ch);
    
    //print output
    echo $doc;
    
    ?>
    
  8. So I think I've understood this as the following.

     

    Current situation: you have 50 crons in your crontab you want to consolidate into one.

     

    If so, something like this could work.

     

    Single cronjob that runs every minute: * * * * * /usr/bin/php -q /path/to/cron.php

     

    cron.php sample contents:

     

    // this comes from either variables or a database
    
    $minute = 0;
    $hour = '*';
    $day = '';
    $month = '';
    
    // if nothing below matches, we won't do anything
    $doirun = FALSE;
    
    if(date("i") == $minute || $minute == '*')
    {
         $doirun = TRUE;
    }
    
    if(date("H") == $hour || $hour == '*')
    {
         $doirun = TRUE;
    }
    
    // and repeat for day, month, week, etc to be as flexible as you want
    
    // now we execute
    
    if($doirun)
    {
          # do your cron action here
    }
    

     

    Time Reference: http://www.php.net/manual/en/function.date.php

  9. Hello,

     

    I am familiar with json and its php counterpart of json_encode and json_decode, similarly serialize and unserialize, however I've recently come across a similar-looking format that doesn't decode with json_decode or unserialize.

     

    I need some help identifying what it is and how to decode it. Any assistance is appreciated!

     

    ["2110761","KZDV","ASDI",1360033414]
    

  10. Hello,

     

    I am familiar with json and its php counterpart of json_encode and json_decode, however I've recently come across a similar-looking format that doesn't decode with json_decode.

     

    I need some help identifying what it is and how to decode it. Any assistance is appreciated!

     

    A snippet:

    a:1:{s:6:"points";a:40:{i:0;a:10:{s:4:"name";s:6:"point0";s:4:"time";s:7:"[19:09]";s:3:"lat";s:16:"44,5283077105488";s:3:"lng";s:16:"11,2966538706291";s:3:"alt";s:1:"0";s:4:"head";s:3:"208";s:2:"gs";s:1:"0";s:5:"phase";s:0:"";s:7:"warning";s:1:"0";s:13:"warningdetail";s:0:"";}i:1;a:10:{s:4:"name";s:6:"point1";s:4:"time";s:7:"[20:40]";s:3:"lat";s:16:"47,4327598955431";s:3:"lng";s:16:"19,2589757429879";s:3:"alt";s:1:"0";s:4:"head";s:3:"129";s:2:"gs";s:1:"0";s:5:"phase";s:7:"Arrived";s:7:"warning";s:1:"0";s:13:"warningdetail";s:0:"";}}}
    

  11. Hello,

     

    I am in the processing of building out an API for a web service and ran into some debates (surprise, surprise...) about the best way for API parameters to be passed.

     

    Option 1:

    domain/service/method/key=value&key2=value2

     

    Option 2:

    domain/service/method/key/value/key2/value

     

    Option 3:

    domain/service/method?key=value&key2=value2

     

    Option 4:

    domain/service/method/key=value/key=value

     

    I know it may seem petty, but I'd like the community's feedback (including if its none of the above and something else!)

     

    Thanks!

  12. Hello,

     

    I have successfully gotten an XML-RPC server up and running (in CodeIgniter) with a flat array. Now I am trying to enhance it by adding nested arrays, however am running into problems.

     

    The output I get from a client is this:

     

    Array
    (
        [start_time] => 1346601240
        [end_time] => 1346608440
        [data] => Array
    ) 

     

    I believe the problem is that the nested arrays are not using ?struct?, but I can?t be sure. The XML-RPC Server code in question:

     

    $data = array();
      foreach($query->result() as $row)
      {
       $data[] = array(
           'valid_time_from' => $row->valid_time_from,
           'valid_time_to' => $row->valid_time_to,
           'hazard_type' => $row->hazard_type,
           'hazard_severity' => $row->hazard_severity,
           'hash' => $row->hash,
           'raw_text' => $row->raw_text,
           );
      }
    
      $response = array(
           array(
             'start_time' => $parameters['0'],
             'end_time' => $parameters['1'],
             'data' => $data
           ),
         'struct');
    
      return $this->xmlrpc->send_response($response); 
    

     

    Any suggestions would be much appreciated!

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