SimpleXML Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • WPPJames
    New Member
    • May 2010
    • 8

    SimpleXML Question

    I'm attempting to use php and SimpleXML to assign variables from an XML output. This works:

    Code:
    $xml = simplexml_load_file($request_url) or die("feed not loading");
    and when I do a:

    Code:
    echo "<pre>";
    var_dump($xml);
    echo "</pre>";
    I get one of these two results depending on value of "Type" attribute.


    Code:
    object(SimpleXMLElement)#1 (2) {
      ["@attributes"]=>
      array(1) {
        ["Type"]=>
        string(5) "Popup"
      }
      ["ad"]=>
      object(SimpleXMLElement)#2 (6) {
        ["url"]=>
        object(SimpleXMLElement)#3 (0) {
        }
        ["cpvrate"]=>
        string(5) "0.005"
        ["creativeid"]=>
        string(10) "0204499385"
        ["campaignid"]=>
        string(12) "2-0210728303"
        ["max-imp-per-user"]=>
        string(1) "1"
        ["frequency-period"]=>
        string(2) "24"
      }
    }
    
    
    object(SimpleXMLElement)#1 (1) {
      ["@attributes"]=>
      array(1) {
        ["Type"]=>
        string(7) "NoPopup"
      }
    }

    In my php code I'm assigning variables like this:

    Code:
    $adtype = (string) $xml->Type;
    $outurl = (string) $xml->ad->url;
    $cpvr = (string) $xml->ad->cpv_rate;
    $crid = (string) $xml->ad->creativeid;
    $caid = (string) $xml->ad->campaignid;
    $mipu = (string) $xml->ad->max-imp-per-user;
    $fp = (string) $xml->ad->frequency-period;
    Only $outurl gets assigned. The other variables remain "empty". What should the correct syntax be for the other variable assignments?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    $xml->ad->max-imp-per-user & $xml->ad->frequency-period are invalid variable names (from the PHP side)

    $xml->ad->cpv_rate to $xml->ad->frequency-period simply don’t exist, because $xml->ad->url is the object that contains these members (i.e. $xml->ad->url->cpv_rate).

    Comment

    • WPPJames
      New Member
      • May 2010
      • 8

      #3
      Thanks Dormilich for the feedback. I managed to get everything working and wanted to post my results for anyone else who happens upon this thread.

      First off, url should have been at the same level as the other elements. Problem was that it was wrapped in a CDATA statement that screwed up the reading in of the file. I corrected that by using:

      Code:
      $xml = simplexml_load_file($request_url,'SimpleXMLElement', LIBXML_NOCDATA)
      After doing that, the variable asignments that worked looked like this:
      Code:
      $adtype = (string) $xml['Type'];
      $outurl = (string) $xml->ad->url;
      $cpvr = (string) $xml->ad->cpvrate;
      $crid = (string) $xml->ad->creativeid;
      $caid = (string) $xml->ad->campaignid;
      $mipu = (string) $xml->ad->{'max-imp-per-user'};
      $fp = (string) $xml->ad->{'frequency-period'};
      Thanks again for pointing me in the right direction.
      Last edited by Dormilich; May 5 '10, 01:23 PM. Reason: Please use [code] tags when posting code

      Comment

      Working...