猿问

如何使用 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' 

任何建议都很好!


aluckdog
浏览 102回答 1
1回答

慕慕森

这让我想起去年玩的一个东西。因为我没有您计划获取的确切值。我将向您展示我使用 cURL 进行操作的示例。应该有帮助。我对我的网站进行了一些更改,所以它可能不再返回任何内容(但谁知道哈哈),但我知道它对我有用,所以重点仍然存在。它的基本要点是 - 输入一个页面,发布搜索的术语,返回页面上的任何内容。除了您想要的之外,这还将向 URL POST 一个值,但您可以跳过 POST 部分。如果数据是在登录或其他东西后面。/*&nbsp;* TESTING GROUNDS&nbsp;*&nbsp;* A. Goal: Search (toms.click/search) and return found articles page&nbsp;* website = toms.click&nbsp;*&nbsp;* word to search for (1 match): axiom&nbsp;*&nbsp;* condition for submit:&nbsp;* if (isset($_POST['searchSubmit']) && isset($_POST['searchbar'])) { ... }&nbsp;* → ['searchSubmit' => 'GO', 'searchbar' => 'axiom']&nbsp;*&nbsp;*&nbsp;* form layout:&nbsp;* <form method="POST" action="https://toms.click/search">&nbsp; &nbsp; &nbsp; &nbsp; <input class="search-bar" type="search" name="searchbar" placeholder="Search" minlength="3" title="search the website" required=""><!--&nbsp; &nbsp; &nbsp; &nbsp; whitespace removal between searchbar and submit&nbsp; &nbsp; &nbsp; &nbsp; --><input class="submit" name="searchSubmit" type="submit" value="Go">&nbsp; &nbsp;</form>&nbsp;*/**&nbsp;* @param $searchbar string whatever you'd type into the searchbar&nbsp;* @return string&nbsp;*/function remoteSearch($searchbar){&nbsp; &nbsp; $url = 'https://toms.click/search'; //The URL of what you want to fetch / enter / post to&nbsp; &nbsp; /** @var array $fields what we're going to post, $fields['a'] = 'b' is $_POST['a'] = 'b' */&nbsp; &nbsp; $fields = array(&nbsp; &nbsp; &nbsp; &nbsp; 'searchSubmit' => 'GO',&nbsp; &nbsp; &nbsp; &nbsp; 'searchbar' => $searchbar&nbsp; &nbsp; );&nbsp; &nbsp; $ch = curl_init();&nbsp; &nbsp; //Set our target url (login script)&nbsp; &nbsp; curl_setopt($ch, CURLOPT_URL, $url);&nbsp; &nbsp; //Enable post and load a post query&nbsp; &nbsp; curl_setopt($ch, CURLOPT_POST, 1);&nbsp; &nbsp; curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));&nbsp; &nbsp; //HTTPs, don't verify it for now&nbsp; &nbsp; curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);&nbsp; &nbsp; curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);&nbsp; &nbsp; //Enable up to 10 redirects&nbsp; &nbsp; curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);&nbsp; &nbsp; curl_setopt($ch, CURLOPT_MAXREDIRS, 10);&nbsp; &nbsp; //We want whatever is on the other side&nbsp; &nbsp; curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);&nbsp; &nbsp; return curl_exec($ch);}你可以用它来轻松抓取东西,所以我想你可以使用它。希望这可以帮助您或为您指明正确的方向:)
随时随地看视频慕课网APP
我要回答