2016-08-03 30 views
0

Hier ist mein Code:DOMXPath/PHP - Wie in foreach Attribut von parrent Knoten erhalten

$url = "http://www.sportsdirect.com/flash-sale"; 
$statuss = 1; 


$curl = curl_init(); 
    curl_setopt($curl, CURLOPT_COOKIE, "ChosenSite=www; SportsDirect_AnonymousUserCurrency=GBP; language=en-GB"); 
    curl_setopt($curl, CURLOPT_URL, $url); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($curl, CURLOPT_SSLVERSION, 3); 
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10); 
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); 
    curl_setopt($curl, CURLOPT_VERBOSE, true); 
    $str = curl_exec($curl); 
    curl_close($curl); 



    libxml_use_internal_errors(true); 
    $doc = new DOMDocument(); 
    $doc->loadHTML($str); 

    $xpath = new DOMXpath($doc); 

    $Sizes = $xpath->query('//ul[@class="bxslider"]//li//a/img'); 
    $AddSlides = $xpath->query('//ul[@class="bxslider2"]//li//a/img'); 
    $AddSlides2 = $xpath->query('//ul[@class="bxslider3"]//li//a/img'); 
    $N = 1; 
    $i = 0; 
    foreach ($Sizes as $item) { 
     $N++; 
     $i++; 
     $ImageAlt = $item->getAttribute("alt"); 
     $Image = $item->getAttribute("src"); 
     $DataImage = $item->getAttribute("data-src"); 

     $Link = $xpath->query('//ul[@class="bxslider"]//li/a')->item(0)->getAttribute("href"); 

     if($DataImage == ""){ 
     echo "<img src='$Image'> LINK: $Link <br>"; 
     //$write->insert("responsivebannerslider_slide", array("group_names" => "1", "titles" => "$ImageAlt", "img_video" => "image", "img_hosting" => "1", "hosted_url" => "$Image", "url" => "$url", "url_target" => "new_window", "url_target" => "new_window", "date_enabled" => "0","sort_order" => "$N", "type" => "1", "statuss" => "$statuss")); 
     } 
     if($DataImage != ""){ 
      //$write->insert("responsivebannerslider_slide", array("group_names" => "1", "titles" => "$ImageAlt", "img_video" => "image", "img_hosting" => "1", "hosted_url" => "http://images.sportsdirect.com$DataImage", "url" => "$url", "url_target" => "new_window", "url_target" => "new_window", "date_enabled" => "0", "sort_order" => "$N", "type" => "1", "statuss" => "$statuss")); 
     echo "<img src='http://images.sportsdirect.com$DataImage'> LINK: $Link <br>"; 
     } 

Hier ist das DOM HTML:

<ul class="bxslider">   
    <li><a href="/flash-sale-three"> 
    <img class="img-responsive" src="//images.sportsdirect.com/images/marketing/flash-sale-slide-160803.jpg" alt="Flash Sale"> 
    </a></li> 

    <li><a href="/pricecrash/price-crash-3"> 
    <img class="img-responsive" data-src="/images/marketing/Price-Crash-160803.jpg" alt="Price Crash"> 
    </a> 
    </li> 

    <li><a href="/special-offer"> 
    <img class="img-responsive" data-src="/images/marketing/weekly-offer-front-site-160721.jpg" alt="Offer Of The Week"> 
    </a> 
    </li> 
</ul> 

Ich bin in der Lage, alle Bilder zu bekommen, aber Ich möchte die Links bekommen, die der Parrent <a href="TARGET-HERE"></a> hält.

Wie soll ich das Attribut parrent node bekommen?

Ich bekomme alle Bilder mit dieser ul[@class="bxslider"]//li//a/img, aber ich kann nicht nur diese ul[@class="bxslider"]//li/a in der foreach-Schleife nehmen und das Attribut href warum?

Ich hoffe, ich habe es gut erklärt, wenn nein bitte frage mich, was du nicht verstehst.

Wo ist mein Fehler?

Antwort

0

Sie können Attribut eines übergeordneten Knoten von $ item nehmen so:

$Link = $item->parentNode->getAttribute("href");