-1

I am writing a bot with php. I want to get images source using curl. I can get img tags but I can't get src value from img. Always I get empty string. Can you see what is my error?

index.php

    $baglan = Baglan("http://www.hurriyetemlak.com/konut-satilik/sakarya-karasu-yali-emlakcidan-apartman-dairesi/detay/23585876");
    preg_match('#<div id="dvSmallPhoto" class="small-photo mt10">                <ul class="thumbs">(.*?)                                    </ul>            </div>#', $baglan, $resimlerGenel);

    preg_match_all('#<li><figure>(.*?)</figure></li>#', $resimlerGenel[1], $resimler);
    $yeni = str_replace('"', '\'', $resimlerGenel[1]);
    preg_match_all("#<li><figure><img src='(.*?)' href='JavaScript:void(0);' onclick='return replaceImg(this)' tabindex='(.*?)' class='pretty' width='114' height='88' alt='(.*?)' /></figure></li>                                    #", $yeni, $resimLinkler);
    print_r($resimLinkler);

Baglan.php

function Baglan($url){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_USER_AGENT, $_SERVER["HTTP_USER_AGENT"]);
    $cikti = curl_exec($curl);
    curl_close($curl);
    return str_replace(array("\n","\t","\r"), null, $cikti);
}

1 Answers1

2

You can probably end up fixing this but regex not the right tool for this.

Instead you should use a DOM parser:

https://secure.php.net/manual/en/class.domxpath.php

It will make your life easier

Here is an example:

https://stackoverflow.com/a/10131137/1880431

Community
  • 1
  • 1
meda
  • 45,103
  • 14
  • 92
  • 122