doulai2025 2016-08-15 05:15
浏览 164
已采纳

如何从textarea表单发布数据?

Here is my current code:

<?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);
?>

My PHP code works, but here's the line I'm having issues with:

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

Basically I just want to have a text-area on my website where users can enter a list of URLs (one per line) and then it Posts it using the PHP code above.

My code works when I just have the URLs built into the code like you can see above, but I just can't figure out how to make it work with a text-area... Any help would be appreciated.

  • 写回答

2条回答 默认 最新

  • duanfuxing2417 2016-08-15 05:22
    关注

    You can do it like below (on the same php page):-

    <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('
    ',$_POST['urls']); //Or $urls=explode('\
    ',$_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);
    }
    ?>
    

    Note:- you can separate the form and logic on two different pages. that's seay i think.

    You can use text-area and then you have to then explode by or if not work then explode by PHP_EOL to the initial url string and rest the code is same as above

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题