1

Through this code I can get the specific text but my problem is how I can get the specific picture on a website through this code. Can anyone help, I am stuck at this point.

<?php

ini_set('max_execution_time', 300);

function news($url,$path){

    $curl=curl_init($url);

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

    $html=curl_exec($curl);

    if(!empty($curl)){

        $thispage= new DOMDocument;
        libxml_use_internal_errors(true);
        $thispage->loadHTML($html);
        libxml_clear_errors();
        $xpath=new DOMXPath($thispage);
        $status=$xpath->evaluate($path);
        return $status;
    }
}

$a= news('https://www.dawn.com/latest-news','string(/html/body/div[2]/div/main/div/div/div[1]/article[1]/h2/a)');

echo $a;

?>
RiggsFolly
  • 83,545
  • 20
  • 96
  • 136
Muslim Mir
  • 13
  • 3

1 Answers1

0

Assuming you want the image the appears next to the first headline, the XPath is:

function news($url,$path){

    $curl=curl_init($url);

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

    $html=curl_exec($curl);

    if(!empty($curl)){

        $thispage= new DOMDocument;
        libxml_use_internal_errors(true);
        $thispage->loadHTML($html);
        libxml_clear_errors();
        $xpath=new DOMXPath($thispage);
        $status=$xpath->evaluate($path);
        return $status;
    }
}

$a= news('https://www.dawn.com/latest-news','string(/html/body/div[2]/div/main/div/div/div[1]/article[1]/figure/div/a/img/@src)');

echo $a;
John C
  • 7,805
  • 2
  • 35
  • 46
  • yes its the xpath for that image but when I include this path in that script it only returns blank page.. it may be because of that 'string(/html...) or anything else, don't know – Muslim Mir Apr 29 '17 at 17:07
  • @MuslimMir check out [this question](http://stackoverflow.com/q/1053424/628267) to help get some errors to display instead of a blank page – John C Apr 29 '17 at 22:53
  • $a= news(' https : //www.dawn.com/latest-news', 'string(/html/body/div[2]/div/main/div/div/div[1]/article[1]/figure/div/a/img/@src ) ' ) ; This line returns me the source of that image.. what shoud i do with the source as I want to save that image into my database – Muslim Mir Apr 30 '17 at 08:08
  • Once you have the URL, [this question](http://stackoverflow.com/q/724391/628267) has a number of examples of how to download the image – John C Apr 30 '17 at 11:46
  • Thank you. Got it.. but one last thing, can i directly save this image to my database instead to a file or folder – Muslim Mir May 01 '17 at 09:03
  • @MuslimMir you can - just create a `blob` field in your database and save the image in there instead of the file. There are some examples in [this question](http://stackoverflow.com/q/11138713) – John C May 01 '17 at 09:25