简体   繁体   English

提供从HTML表单到PHP数组的URL列表

[英]Feeding a list of URLs from an HTML form to PHP array

I have a .php file which I will be using to submit requests to a certain API. 我有一个.php文件,我将用它来向某个API提交请求。 This API will return information regarding certain domain URLs, such as the domains age, PageRank, etc. 此API将返回有关某些域名网址的信息,例如域名年龄,PageRank等。

The part of the PHP file which is responsible for feeding the API call URL with the domain names I'm interested in looks as follows: PHP文件中负责为API调用URL提供我感兴趣的域名的部分如下所示:

$batchedDomains = array('www.example.com', 'www.cnn.com', 'www.apple.com'); 

What I would like to do is feed this array information through a very simple HTML form. 我想要做的是通过一个非常简单的HTML表单提供这个数组信息。 My current HTML for the form looks as follows: 我目前的表单HTML如下所示:

<form name="myform" action="apitest.php" method="POST">
    <input type="hidden" name="check_submit" value="1" />

    URL List:<br /> 
    <textarea name="urls" rows="20" cols="60">Enter URLs</textarea><br />
    <input type="submit" />
</form>

Here is what I would like to see happen: whenever I enter a list of domain URLs into the HTML form (one domain per line), I would like the $batchedDomains array to be populated with those values. 以下是我希望看到的情况:每当我在HTML表单中输入域URL列表(每行一个域)时,我希望$ batchedDomains数组填充这些值。

Can anyone help me out with this? 任何人都可以帮我解决这个问题吗? Or if you have a suggestion for a different solution I'm of course willing to hear it out. 或者如果您对不同的解决方案有建议,我当然愿意听取它。

I do not want this information printed anywhere, as it will simply be used by the php script to call the API and display the results. 我不希望在任何地方打印此信息,因为它将简单地由php脚本用于调用API并显示结果。

Thank you. 谢谢。

$urls = array_filter(explode(PHP_EOL, $_POST['urls']), 'parse_url');

或者使用filter_var() + FILTER_VALIDATE_URL传递自定义回调以进行更严格的检查

如果您只是每行输入一个,可以按换行分割textarea字符串,请参阅https://stackoverflow.com/a/1483501/2213444

<?php
// has no error checking, you'll want to check that $_POST['urls'] exists
$textarea = explode("\r\n", $_POST['urls']);
print_r($textarea);

?>
    <form action="" method="post">
        <textarea name='urls' rows='20' cols='60'>
        </textarea>
        <input type="submit">
    </form>

Working Example 工作实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM