Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用cURL从页面获取文本_Php_Curl - Fatal编程技术网

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

Php 如何使用cURL从页面获取文本,php,curl,Php,Curl,我最近需要让一个PHP文件从一个页面获取文本,并显示它,但我不知道怎么做 我目前的代码是: 任何建议都是好的 这让我想起了去年玩的东西。因为我没有您计划获取的确切值。。我将向您展示一个使用cURL的示例。这应该会有帮助 我对我的网站做了一点改动,所以它可能不再返回任何内容(但谁知道哈哈),但我知道它对我有用,所以这一点仍然存在 它的基本要点是——输入一个页面,发布搜索词,返回页面上的任何内容。除了您想要的内容之外,这将向URL发布一个值,但是您可以跳过发布部分。如果数据在登录或其他信息之后 /

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

我目前的代码是:


任何建议都是好的

这让我想起了去年玩的东西。因为我没有您计划获取的确切值。。我将向您展示一个使用cURL的示例。这应该会有帮助

我对我的网站做了一点改动,所以它可能不再返回任何内容(但谁知道哈哈),但我知道它对我有用,所以这一点仍然存在

它的基本要点是——输入一个页面,发布搜索词,返回页面上的任何内容。除了您想要的内容之外,这将向URL发布一个值,但是您可以跳过发布部分。如果数据在登录或其他信息之后

/*
 * 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);
}
/*
*试验场
*
*目标:搜索(toms.click/Search)并返回找到的文章页面
*website=toms.click
*
*要搜索的单词(1个匹配项):axiom
*
*提交条件:
*if(isset($_POST['searchSubmit'])和&isset($_POST['searchbar']){…}
* → ['searchSubmit'=>'GO','searchbar'=>'axiom']
*
*
*表格布局:
* 
*
/**
*@param$searchbar字符串,无论您在搜索栏中键入什么
*@返回字符串
*/
函数remoteSearch($searchbar)
{
$url='1https://toms.click/search“;//要获取/输入/发布到的内容的URL
/**@var array$fields我们要发布的内容,$fields['a']='b'是$\u post['a']='b'*/
$fields=数组(
“searchSubmit”=>“GO”,
“搜索栏”=>$searchbar
);
$ch=curl_init();
//设置我们的目标url(登录脚本)
curl_setopt($ch,CURLOPT_URL,$URL);
//启用post并加载post查询
卷曲设置($ch,卷曲设置桩,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($fields));
//HTTPs,暂时不验证它
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
//最多可启用10个重定向
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch,CURLOPT_MAXREDIRS,10);
//我们想要另一边的东西
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
返回curl_exec($ch);
}
你可以用它来轻松抓取东西,所以我想你可以用它


希望这有助于或为您指出正确的方向:)

“这应该被提取”-从哪里提取?我也会托管要提取的文本。这并不能回答问题。从哪里开始-文本文件、数据库、HTTP端点…?在我们可能帮助你之前,你至少需要先弄清楚你到底想要什么。一旦你决定了这一点,你也应该先做更多的研究——“不知道怎么做”对于在这个网站上提问来说,努力太少了。请去看看。非常感谢!这正是我所需要的。嗨@安达里Axisto,很高兴有帮助,如果这解决了你的问题,请通过点击复选标记来考虑。这向更广泛的社区表明,你已经找到了一个解决方案,并给回答者和你自己带来了一些声誉。没有义务这样做。欢迎使用堆栈溢出:)
/*
 * 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);
}