PHP help get data from xml

Discussion in 'PHP' started by Amy Miller, Nov 11, 2013.

  1. #1
    I have this code to get an offer feed:

          $xml = simplexml_load_file("http://xxxxxx.com/?token=123456789");
         
    /*
    <MyOffers>
      <Campaign ID="1234512" OfferTitle="Offer title">
        <Description>Offer description</Description>
        <Payout>$1.50</Payout>
        <TrafficTypes>IsNetwork,Email,Display,Search,Incentive,Social,Contextual,CPC</TrafficTypes>
        <Countries>US</Countries>
        <StartDate>10/29/2013</StartDate>
        <EndDate>10/29/2015</EndDate>
        <Status>Live</Status>
        <LandingPagePreview><![CDATA[http://asdasdadasdsa.com/site.one?CID=123&AFID=-1&ADID=-1&SID=&DO_NOT_USE_FOR_TRACKING]]></LandingPagePreview>
        <CampaignType>CPA</CampaignType>
        <Category>Survey/Full Path</Category>
      </Campaign>
    </MyOffers>
    */
    
        // $offers = array();
       
          foreach ($xml->Campaign as $offer){
    
          $country = $offer->Countries;
         
          if(empty($offer->LandingPagePreview))
          {
            continue;         
          }
    
          $name = $offer->OfferTitle;
    
          $name = strip_tags($name);
          $name = trim($name);
         
          $desc = '';
          $desc = $offer->description;
          $desc = html_entity_decode($desc);
          $desc = strip_tags($desc);
         
          $desc = str_ireplace("<br>", " ", $desc);
         
          $urlul = '';
          $urlul = $offer->LandingPagePreview;
          $urlul = strip_tags($urlul);
         
                     
          $oid = (string) $offer->ID;
          $offers[$oid]['id'] = $oid;
          $offers[$oid]['name'] = $name;
          $offers[$oid]['url'] = $urlul;
          $offers[$oid]['description'] = $desc;
          $offers[$oid]['country'] = $country;
          $offers[$oid]['payout'] = str_replace("$","",$offer->Payout);
          $offers[$oid]['epc'] = '0.00';     
         
    
    
         
        }
         
        $offer = null;
    PHP:
    The problems are:

    1. I can't get the ID and OfferTitle right

    2. I have to get the url from <LandingPagePreview> only like this
    http://asdasdadasdsa.com/site.one?CID=123
    Code (markup):
    without the extra data there (CDATA and the other variables in the url).


    Please help me!
     
    Amy Miller, Nov 11, 2013 IP
  2. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #2
    I wouldn't use simplexml. I am a lover of preg_match_all

    so this is what I would do.

    $xml = file_get_contents('http://xxxxxx.com/?token=123456789');
    
    // remove line feeds
    $xml = str_replace("\n", ' ', $xml);
    
    preg_match_all('|Campaign ID="([0-9]*?)"|',$xml,$ids);
    preg_match_all('|<LandingPagePreview><!\[CDATA\[(.*?)\]\]></LandingPagePreview>|',$xml, $landingPages);
    
    // debug output
    print_r($ids[1]);
    print_r($landingPages[1]);
    
    PHP:
     
    stephan2307, Nov 11, 2013 IP
  3. Amy Miller

    Amy Miller Member

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    31
    #3
    Nice solution... but can't you give me a solution for the current form?
     
    Amy Miller, Nov 11, 2013 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    You can get the ID and title this way:
    
    foreach ($xml->Campaign AS $offer)
    {
        $title = $offer['OfferTitle'];
        $id = $offer['ID'];
    
        // ...
    }
    
    PHP:
    As for the CDATA, you have to add the LIBXML_NOCDATA option to simplexml_load_file():
    
    $xml=simplexml_load_file("http://xxxxxx.com/?token=123456789", null, LIBXML_NOCDATA);
    
    PHP:
    EDIT:
    To remove the variables except the first from the URL:
    
    $url = explode('&', $xml->Campaign->LandingPagePreview, 2)[0];
    
    PHP:
    ... assuming CID is always the first variable.
     
    Last edited: Nov 11, 2013
    nico_swd, Nov 11, 2013 IP
  5. Amy Miller

    Amy Miller Member

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    31
    #5
    Fom this:

    <LandingPagePreview><![CDATA[http://asdasdadasdsa.com/site.one?CID=123&AFID=-1&ADID=-1&SID=&DO_NOT_USE_FOR_TRACKING]]></LandingPagePreview>
    PHP:
    I need to get the url something like $urlul = $offer->LandingPagePreview but to strip it of <![CDATA[ and &AFID=-1&ADID=-1&SID=&DO_NOT_USE_FOR_TRACKING]]> so the result would be

    http://asdasdadasdsa.com/site.one?CID=123
    PHP:
     
    Amy Miller, Nov 11, 2013 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    I edited my post above. Tested and working. Don't forget to add the LIBXML_NOCDATA to your first line.
     
    nico_swd, Nov 11, 2013 IP