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条)

报告相同问题?

悬赏问题

  • ¥25 关于##爬虫##的问题,如何解决?:
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误