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!
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:
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.
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:
I edited my post above. Tested and working. Don't forget to add the LIBXML_NOCDATA to your first line.