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;

?>
1
  • 1
    Good code indentation make it easier to read, and more importantly, easier for you to debug Commented Apr 28, 2017 at 22:10

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

6 Comments

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
@MuslimMir check out this question to help get some errors to display instead of a blank page
$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
Once you have the URL, this question has a number of examples of how to download the image
Thank you. Got it.. but one last thing, can i directly save this image to my database instead to a file or folder
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.