Jump to content

salathe

Staff Alumni
  • Posts

    1,832
  • Joined

  • Last visited

  • Days Won

    3

Posts posted by salathe

  1. Working with namespaced elements is annoying, with SimpleXML. You can recognise namespaces by xmlns:...="..." attributes, and <foo:name tags.

     

    A better option would be to use the DOM family of classes and functions.

     

    Here's a basic example that grabs the <ax21:...> elements from your XML and throws them into an array.

     

    <?php
    
    $xml = <<<XML
    <?xml version="1.0" encoding="UTF-8"?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
    <ns:getReportTestResponse xmlns:ns="http://webservice.avm.pvads.com">
    <ns:return xmlns:ax21="http://webservice.avm.pvads.com/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax21:WSReportBean">
    <ax21:confidence />
    <ax21:dateStamp>12/07/2007</ax21:dateStamp>
    <ax21:html>adsasdasdsadasd</ax21:html>
    <ax21:pdsReference />
    <ax21:product>AVi (Exterior AND Interior Inspection) - 48hr RUSH</ax21:product>
    <ax21:sourceData />
    <ax21:successful>true</ax21:successful>
    <ax21:value>168000.0</ax21:value>
    </ns:return>
    </ns:getReportTestResponse>
    </soapenv:Body>
    </soapenv:Envelope>
    XML;
    
    $document = new DOMDocument;
    $document->loadXML($xml);
    
    $ax21_elements = $document->getElementsByTagNameNS("http://webservice.avm.pvads.com/xsd", "*");
    
    $array = array();
    foreach ($ax21_elements as $element) {
        $array[$element->localName] = trim($element->textContent);
    }
    var_dump($array);
    
    That will output an array like:

     

    array( {
      ["confidence"]=>
      string(0) ""
      ["dateStamp"]=>
      string(10) "12/07/2007"
      ["html"]=>
      string(15) "adsasdasdsadasd"
      ["pdsReference"]=>
      string(0) ""
      ["product"]=>
      string(50) "AVi (Exterior AND Interior Inspection) - 48hr RUSH"
      ["sourceData"]=>
      string(0) ""
      ["successful"]=>
      string(4) "true"
      ["value"]=>
      string( "168000.0"
    }
    
    That said, there's usually no real need to "convert" XML into an array at all. Instead, work with the DOM objects (here $document, $ax21_elements, and $element are DOM objects).
  2. $classname = 'class.validator';
     
    function __autoload($classname) {
        $filename = $classname . '.php';
        require_once($filename);
    }
     
    $validator = new Validator;
    
    That's not how autoloading works. The idea is what when your code tries to use a class that PHP has not loaded yet, the autoloader function provides the opportunity for your script to load the correct file before PHP finally gives up and barfs out an error.

     

    In your script, upon reaching the "new Validator" part, PHP knows that the class has not been loaded yet. You have defined an autoloader so it gets called with the class name: effectively calling __autoload("Validator").

     

    Looking inside your function, hopefully you can see that $filename will become Validator.php, which is not the correct file name to be loading (you wanted class.validator.php).

     

    You probably want to make the first line of the function be like $filename = "class." . strtolower($classname) . ".php";

  3. The array_key_exists() function returns whether the array has the specified key, or not. To steal a comment from the manual, "An array in PHP is … a type that associates values to keys." (http://php.net/arrays). In your example, your array has only one value "a" and its key is 0.

     

    It looks like the function you really wanted was in_array() (http://php.net/in_array), which returns whether the array as the specified value, or not.

  4. Ah I see the difference. It doesn't use //prefix because the element isn't a namespace right? … Guess it just depends on the way the XML is formatted? Well you learn something everyday.

    That's right, the <item> elements that you want are not within a namespace so there is no need to register a prefix nor use that prefix in the XPath query.

     

    If you decided not to use XPath at all, an alternative would be like:

     

    foreach ($entries->channel->item as $entry) …
    
  5. last day of 2013-05-31 +1months

    The three different date parts have the following effect:

    • 2013-05-31 is evaluated: internal date 2013-05-31 (at midnight)
    • +1 months is evaluated: internal date 2013-06-31 (that's right, June 31st) the logic is simply to increment the month number without touching days
    • last day of is evaluated: internal date 2013-06-30 (June only has 30 days)
    It is only after all of these steps that the date, if it is invalid, is tweaked to become valid. For example, if we didn't have "last day of" then it would have rolled around to July 1st.

     

    Aside: in this case, it is not important which order the three parts are written.

  6. You're using an old version of PHP (less than 5.3.0) that does not allow the const keyword outside of a class definition.

     

    Change them to use define() instead.

     

    const REDIRECT_URL = 'INSERT YOUR REDIRECT URL HERE';
    // change the above to this:
    define('REDIRECT_URL', 'INSERT YOUR REDIRECT URL HERE');
    
    There is a very good chance that there are other parts of the code which require PHP 5.3.0 or greater, so these constants might not be the only thing that you need to change to get the script to work on an older version of PHP.
  7. The LIST command that you're sending via the PHP script is different to that that is expected (and the one you demonstrated with telnet).  The PHP script sends:

    C02 LIST '' '*'

    When it should send:

    C02 LIST "" "*"

    See the difference?  Something like the following would suffice:

    fwrite($fp, 'C02 LIST "" "*"' . "\r\n"); 
  8.  

    $date = date('Y0101', strtotime(2000));

    But there is nothing wrong with what the above poster gave. Either will work fine.

    Nothing wrong at all, except it will print the value for January 1st of the current year. When given to strtotime(), the 2000 is interpreted as a 24-hour time: 8pm.

     


    I just thought there would be a "proper" way to do it, rather than what is effectively a hack.

    A good way would be something like the following, which uses DateTime::createFromFormat().

     

    $date = DateTime::createFromFormat('!Y', $year)->format('Ymd');

     

    If you're still stuck on PHP 5.2 or lower (I'm sorry!) then mktime() is an alternative.

     

    $date = date('Ymd', mktime(0, 0, 0, 1, 1, $year));
  9. Having never seen the syntax before I'm surprised that

    var pos = months.indexOf( birthMonth.substring( (0, 3 /3) /3 ).toLowerCase() );
    is valid, but apparently it is. Anyway, looks like you repeated yourself a bit too much in that call to .substring().

     

     

    The comma operator is a very esoteric operator, rarely seen out in the wild. I'm not surprised that you're surprised that it is valid. Its use is discouraged.
×
×
  • 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.