如何使用cURL从页面获取文本

时间:2020-08-27 14:10:16

标签: php curl

我最近需要制作一个PHP文件来从页面中获取文本并显示它,但我不知道该怎么做。

我当前的代码是:

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' 

任何建议都很好!

1 个答案:

答案 0 :(得分:1)

这让我想起了去年我在玩的游戏。由于我没有您打算提取的确切值,因此我将向您展示一个使用cURL的示例。应该会有帮助。

我稍微改变了网站,所以它可能不再返回任何东西(但是谁知道哈哈),但是我知道它为我工作了,所以重点仍然是在那里。

它的基本要点是-进入页面,发布搜索词,返回页面上的所有内容。除了您要的内容外,这还将向URL发布一个值,但是您可以跳过POST部分。如果数据在登录名或其他内容后面。

/*
 * 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);
}

您可以使用它轻松地抓取东西,所以我想您可以使用它。

希望这对您有帮助或指向正确的方向:)

相关问题