Search code examples
phpdomcurlweb-crawlerdomcrawler

Curl is returning a string


I'm using curl to get my values from a site name PKNiC

My code is:

function _isCurl() {
    return function_exists('curl_version');
}

if (_iscurl()) {
    //curl is enabled
    $url = "https://pk6.pknic.net.pk/pk5/lookup.PK?name=cat.com.pk&jsonp=?";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    curl_close($ch);
    var_dump($output);
    // Curl operations finished
} else {
    echo "CURL is disabled";
}

Now when I run this program it returns a string to me with whole page print on it as a single string.

enter image description here

I need registrant name, expiry date, create date, contacts. How do I get those things? I have no idea how it works and it just provide me a single string when I use var_dump or print_r or any thing to view it. How to get the record of my choice?


Solution

  • Use a DOM Crawler, like this one: http://symfony.com/doc/current/components/dom_crawler.html.

    Then you can get the registrant name like this:

    use Symfony\Component\DomCrawler\Crawler;
    
    $crawler = new Crawler($htmlFromCurl);
    
    $crawler = $crawler->filter('.whitebox tr:nth-child(3) td:last-child');
    

    Filtering is even easier if you have the CssSelector component installed. This allows you to use jQuery-like selectors to traverse.

    You can install the Dom Crawler without using the whole framework

    composer require symfony/dom-crawler