andri axysto
andri axysto

Reputation: 19

How to fetch text from a page using cURL

I recently needed to make a PHP file fetch the text from a page, and display it but I did not know how to do it.

My current code is:

https://pastebin.com/Zhh4SS3L

        $results["registeredname"] = "here shall be the domain";
    $results["productname"] = "this shall be fetched";
    $results["productid"] = "5";
    $results["billingcycle"] = "Monthly";
    $results["validdomains"] = $this->getHostDomain();
    $results["validips"] = $this->getHostIP();
    $results["validdirs"] = $this->getHostDir();
    $results["checkdate"] = Carbon::now()->toDateString();
    $results["version"] = "this shall be fetched";
    $results["regdate"] = "this shall be fetched";
    $results["nextduedate"] ="this shall be fetched";;
    $results["addons"] = array(array('name' => 'Branding Removal', 'nextduedate' => "this shall be fetched";', 'status' 

Any advices are good!

Upvotes: 0

Views: 302

Answers (1)

Morgosus
Morgosus

Reputation: 827

this reminds me of something I was playing with last year. As I do not have the exact values you're planning to fetch.. I'll show you an example of what I was playing with using cURL. It should help.

I changed my website a bit, so it probably doesn't return anything anymore (but who knows haha), but I know for a fact that it worked for me, so the point is still there.

The basic gist of it was - enter a page, post searched term, return whatever was on the page. In addition to what you want, this will POST a value to the URL, but you can skip the POST part. If the data is behind a login or something.

/*
 * TESTING GROUNDS
 *
 * A. Goal: Search (toms.click/search) and return found articles page
 * website = toms.click
 *
 * word to search for (1 match): axiom
 *
 * condition for submit:
 * if (isset($_POST['searchSubmit']) && isset($_POST['searchbar'])) { ... }
 * → ['searchSubmit' => 'GO', 'searchbar' => 'axiom']
 *
 *
 * form layout:
 * <form method="POST" action="https://toms.click/search">
        <input class="search-bar" type="search" name="searchbar" placeholder="Search" minlength="3" title="search the website" required=""><!--
        whitespace removal between searchbar and submit
        --><input class="submit" name="searchSubmit" type="submit" value="Go">
   </form>
 *


/**
 * @param $searchbar string whatever you'd type into the searchbar
 * @return string
 */
function remoteSearch($searchbar)
{
    $url = 'https://toms.click/search'; //The URL of what you want to fetch / enter / post to

    /** @var array $fields what we're going to post, $fields['a'] = 'b' is $_POST['a'] = 'b' */
    $fields = array(
        'searchSubmit' => 'GO',
        'searchbar' => $searchbar
    );

    $ch = curl_init();

    //Set our target url (login script)
    curl_setopt($ch, CURLOPT_URL, $url);

    //Enable post and load a post query
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));

    //HTTPs, don't verify it for now
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

    //Enable up to 10 redirects
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 10);

    //We want whatever is on the other side
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    return curl_exec($ch);
}

You can use this to easily grab stuff, so I guess you could use it.

Hope this helps or points you in the right direction :)

Upvotes: 1

Related Questions