这是我目前的代码:
<?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时,我的代码可以正常工作,就像您在上面看到的那样,但我无法弄清楚如何使其与文本区域一起工作...任何帮助将不胜感激
答案 0 :(得分:2)
你可以像下面那样(在同一个php页面上): -
MatLab
注意: - 您可以在两个不同的页面上分离表单和逻辑。我想是那个。“
您可以使用文本区域,然后必须按<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
将其分解为初始url字符串,并将代码与上面相同
答案 1 :(得分:1)
您可以传递给表格值,如下面的框中所示。 HTML表单中的任何标记都有name属性,为任何textarea指定名称,输入或选择标记。
<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);