如何使用php-ccurl从另一个网站进行搜索和输出


How to use php curl in searching and outputting from another website?

我是网络开发领域的新手。请原谅我犯了一些错误和知识不足。

我的问题是如何在这个问题中使用php-cUrl,我制作了一个php文件,作为另一个网站的索引或首页。我们被要求做的是创建一个搜索页面,然后输出搜索结果。用户输入一些东西,然后在其他网站上搜索,然后结果可能会输出到一些iframe上。

下面是我的代码,

<?php
$url = 'http://somesite.com';
{
$username=$_POST['Search'];
$yourformfields="Search=$Search";
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,'http://somesite.com');
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl_handle,CURLOPT_POST,1);
curl_setopt($curl_handle,CURLOPT_POSTFIELDS,$yourformfields);
$buffer = curl_exec($curl_handle);
//$result = curl_exec($curl_handle);
curl_close($curl_handle);
if (empty($buffer))
{
    echo"<iframe src='$buffer'></iframe>";
}

}
?>
    <form action="index.php" method="post">
    <input type="text" name="formName" value="<?=$varName;?>">
    <input type="Submit" name="formSubmit" value="Search">
    </form>

当我输入一些东西时,它不会输出结果,老实说,它什么都不输出。我已经检查了四五次了,但由于我目前在网络开发和编程方面的知识,我似乎找不到问题。感谢tnx的所有帮助。

这应该会给您一个良好的开端,在您尝试使用ajax的情况下,iframe不是一个好的解决方案。

curl.php

    <form action="curl.php" method="post">
        URL <input type="text" name="url" value="<?php echo ((isset($_POST['url']))?$_POST['url']:"") ?>"> <br>
        Keyword <input type="text" name="keyword" value="<?php echo ((isset($_POST['keyword']))?$_POST['keyword']:"") ?>"> <br>
        <input type="Submit" name="formSubmit" value="Search">
    </form>
<?php
if(isset($_POST['formSubmit']))
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $_POST['url']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $st = curl_getinfo($ch, CURLINFO_HTTP_CODE);  
    $response = curl_exec($ch);
    if($st==200)
       echo "Request Unsuccessful";
    else
    {
        if (strpos($response,$_POST['keyword']) !== false)
        {
            echo 'Your input keyword found.';
        }
        else
            echo 'No matches, try another keyword';
    }
    curl_close($ch);
}
?>

注意:此代码要求您已启用php curl扩展