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

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。