【问题标题】:how to insert form query into $html = file_get_contents_curl();如何将表单查询插入 $html = file_get_contents_curl();
【发布时间】:2015-05-05 17:39:03
【问题描述】:

您好,我正在尝试使用file_get_contents_curl() 中的表单提交数据

我想制作一个表单,您可以在其中提交网址并在该网站上查找元标记

这是我的代码:

<form method="post" action="index.php">
<input type="text" name="test">
<input type="submit">
</form></center>

<?php
function file_get_contents_curl($url)
{
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}

$html = file_get_contents_curl($_POST["test"];); // the submitted url should go here
$doc = new DOMDocument();
@$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');

$title = $nodes->item(0)->nodeValue;

$metas = $doc->getElementsByTagName('meta');

for ($i = 0; $i < $metas->length; $i++)
{
    $meta = $metas->item($i);
    if($meta->getAttribute('name') == 'title')
        $description = $meta->getAttribute('content');
    if($meta->getAttribute('name') == 'description')
        $description = $meta->getAttribute('content');
    if($meta->getAttribute('name') == 'keywords')
        $keywords = $meta->getAttribute('content');
}


echo '<br/><br/>';
echo "Title: $title". '<br/><br/>';
echo "Description: $description". '<br/><br/>';
echo "Keywords: $keywords". '<br/><br/>';
?>

当我手动输入地址时,一切都很好,例如。

$html = file_get_contents_curl('skynews.com');

但这根本不起作用:

$html = file_get_contents_curl($_POST["test"];);

如何调用这个变量?

【问题讨论】:

    标签: php html forms variables file-get-contents


    【解决方案1】:

    好的,我终于想通了!

    我必须先添加一个变量

    <?php $address = $_POST["test"];?>
    

    $html = file_get_contents_curl($address);
    

    效果很好

    【讨论】:

    • 这肯定不是问题,因为在这种情况下使用额外的变量是多余的。可能错误是/是盈余; 这里:$html = file_get_contents_curl($_POST["test"];); 使:$html = file_get_contents_curl($_POST["test"]);
    猜你喜欢
    • 1970-01-01
    • 2021-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 2020-02-04
    相关资源
    最近更新 更多