Tag Archive for simplexml

objects into array

function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
    $arrData = array();
   
    // if input is object, convert into array
    if (is_object($arrObjData)) {
        $arrObjData = get_object_vars($arrObjData);
    }
   
    if (is_array($arrObjData)) {
        foreach ($arrObjData as $index => $value) {
            if (is_object($value) || is_array($value)) {
                $value = objectsIntoArray($value, $arrSkipIndices); // recursive call
            }
            if (in_array($index, $arrSkipIndices)) {
                continue;
            }
            $arrData[$index] = $value;
        }
    }
    return $arrData;
}

source

Print Xml atttributes through simplexml

Print Atrributes of a Xml object through simplexml

$results->attributes()->q; (if q is the attribute)

source

SMS/Email Weather Alert

<?php
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'http://www.weather.gov/alerts/wwarssget.php?zone=TNZ027');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$contents = curl_exec($ch);
curl_close($ch);

if(!empty($contents)) {

$db = new SQLiteDatabase('noaa.db2');
/*
$time = time();
$db->query("BEGIN;
CREATE TABLE status ( id INTEGER PRIMARY KEY, timestamp );
INSERT INTO status (timestamp) VALUES($time);
COMMIT;");
*/
$result = $db->query('SELECT * FROM status WHERE id = 1 LIMIT 1',SQLITE_ASSOC); //SQLITE_NUM SQLITE_BOTH (Default)
$data = $result->current();
//echo $data['timestamp'];

$xml = new SimpleXMLElement($contents);
foreach($xml->channel->item as $item) {

if (strstr(trim($item->title),'Tornado Warning')) {

if($data['timestamp'] < (time()-1800)) { // Set time in seconds between alerts

$db->query("UPDATE status SET timestamp = ".time()." WHERE id = 1");

require("phpmailer/class.phpmailer.php"); //http://sourceforge.net/projects/phpmailer
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->Username = "xxxx";  // SMTP username
$mail->Password = "xxxx"; // SMTP password
$mail->SetLanguage("en",dirname(__FILE__) . "/phpmailer/language/");
$mail->From = "alerts@johnself.com";
$mail->FromName = "John Self";
$mail->AddAddress("email1@internet.com");
//$mail->AddAddress("email2@internet.com"); // Add as many recipients as you want
$mail->IsHTML(false);
$mail->Subject = "WEATHER ALERT";
$mail->Body    = "A TORNADO WARNING HAS BEEN ISSUED FOR DAVIDSON COUNTY";
$mail->AltBody = "A TORNADO WARNING HAS BEEN ISSUED FOR DAVIDSON COUNTY";

if(!$mail->Send())
{
echo "Message could not be sent.
";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
exit;
}
}
}
}
?>

source

Convert PHP Array to XML or Simple XML Object if you wish

class ArrayToXML
{
/**
* The main function for converting to an XML document.
* Pass in a multi dimensional array and this recrusively loops through and builds up an XML document.
*
* @param array $data
* @param string $rootNodeName - what you want the root node to be - defaultsto data.
* @param SimpleXMLElement $xml - should only be used recursively
* @return string XML
*/
public static function toXml($data, $rootNodeName = 'data', $xml=null)
{
// turn off compatibility mode as simple xml throws a wobbly if you don't.
if (ini_get('zend.ze1_compatibility_mode') == 1)
{
ini_set ('zend.ze1_compatibility_mode', 0);
}

if ($xml == null)
{
$xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$rootNodeName />");
}

// loop through the data passed in.
foreach($data as $key => $value)
{
// no numeric keys in our xml please!
if (is_numeric($key))
{
// make string key...
$key = "unknownNode_". (string) $key;
}

// replace anything not alpha numeric
$key = preg_replace('/[^a-z]/i', '', $key);

// if there is another array found recrusively call this function
if (is_array($value))
{
$node = $xml->addChild($key);
// recrusive call.
ArrayToXML::toXml($value, $rootNodeName, $node);
}
else
{
// add single node.
$value = htmlentities($value);
$xml->addChild($key,$value);
}

}
// pass back as string. or simple xml object if you want!
return $xml->asXML();
}
}

source