如何从文本区域表单中发布数据


How to POST data from a textarea form?

下面是我当前的代码:

<?php
$apikey='IGNORE-THIS-VARIABLE';
// All URLS to be sent are hold in an array for example
$urls=array('http://www.site1.com','http://www.site2.com/');
// build the POST query string and join the URLs array with | (single pipe)
$qstring='apikey='.$apikey.'&urls='.urlencode(implode('|',$urls));
// Do the API Request using CURL functions
$ch = curl_init();
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_URL,'http://www.example.com/api.php');
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_TIMEOUT,40);
curl_setopt($ch,CURLOPT_POSTFIELDS,$qstring);
curl_exec($ch);
curl_close($ch);
?>

我的PHP代码工作,但这里是我有问题的行:

$urls=array('http://www.site1.com','http://www.site2.com/');

基本上,我只是想在我的网站上有一个文本区域,用户可以输入url列表(每行一个),然后使用上面的PHP代码发布它。

我的代码工作时,我只是有url内置于代码,就像你可以看到上面,但我只是不知道如何使它与文本区工作…

你可以这样做(在同一个php页面上):-

<form method = "POST">
<textarea name="urls"></textarea><!-- add a lable that please enter new line separated urls -->
<input type = "submit">
</form>
<?php
if(isset($_POST['urls'])){
    $apikey='IGNORE-THIS-VARIABLE';
    // All URLS to be sent as new-line separated string  and explode it to an array
    $urls=explode(''n',$_POST['urls']); //Or $urls=explode('''n',$_POST['urls']);
    // if not worked then 
    //$urls=explode(PHP_EOL,$_POST['urls']); 
    // build the POST query string and join the URLs array with | (single pipe)
    $qstring='apikey='.$apikey.'&urls='.urlencode(implode('|',$urls));
    // Do the API Request using CURL functions
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch,CURLOPT_URL,'http://www.example.com/api.php');
    curl_setopt($ch,CURLOPT_HEADER,0);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_TIMEOUT,40);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$qstring);
    curl_exec($ch);
    curl_close($ch);
}
?>

注意:-你可以把表单和逻辑分开放在两个不同的页面上。我是这么想的。

你可以使用文本区域然后你必须用'n爆炸或者如果不工作,那么用PHP_EOL爆炸到初始url字符串剩下的代码和上面一样

您可以像下面的框一样传递给表单值。HTML表单中的任何标签都有name属性,为任何文本区域,输入或选择标签指定名称。

<form method="post" name="first_form">
    <input type="text" name="url" value="" />
    <textarea name="note"></textarea>
    <input type="submit" value="Post" />
</form>

在PHP中,您可以使用两个方法传递值,$_GET或$_POST。指定标签名称,你给HTML标签($_POST['note']或$_POST['url']),也可以使它像一个数组$_POST。

<?php
  if(isset($_POST)){
    // display the posted values from the HTML Form
    echo $_POST['note'];
    echo $_POST['url'];
  }
?>
代码:

    $apikey='IGNORE-THIS-VARIABLE';
    // All URLS to be sent are hold in an array for example
    $urls=array('http://www.site1.com','http://www.site2.com/');
    // build the POST query string and join the URLs array with | (single pipe)
    $qstring='apikey='.$apikey.'&urls='.urlencode(implode('|',$urls));
    // Do the API Request using CURL functions
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch,CURLOPT_URL,'http://www.example.com/api.php');
    curl_setopt($ch,CURLOPT_HEADER,0);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_TIMEOUT,40);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$qstring);
    curl_exec($ch);
    curl_close($ch);