欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

批量生成url

程序员文章站 2022-05-29 21:24:54
...
下面是百度短网址接口生成代码
$ch=curl_init();curl_setopt($ch,CURLOPT_URL,"http://dwz.cn/create.php");curl_setopt($ch,CURLOPT_POST,true);curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);$data=array('url'=>'http://www.baidu.com/');curl_setopt($ch,CURLOPT_POSTFIELDS,$data);$strRes=curl_exec($ch);curl_close($ch);$arrResponse=json_decode($strRes,true);if($arrResponse['status']==0){/**错误处理*/echo iconv('UTF-8','GBK',$arrResponse['err_msg'])."\n";}/** tinyurl */echo$arrResponse['tinyurl']."\n";

这样子只能生成单条.有没办法以序列批量生成输出.

因为我想要的效果是能批量生成
www.xxx.cn/?post=1
www.xxx.cn/?post=2
www.xxx.cn/?post=3
......
www.xxx.cn/?post=100
www.xxx.cn/?post=101
序列通通转换成短网址.


回复讨论(解决方案)

把你的代码改写为函数,并将 data 数组作为参数传入

不会程序呢.能不能帮忙解决下.谢谢了.

$num = 10;
$url = "http://www.xxx.cn?post=";
$sourceAry = array();
$destArray = array();
for($i = 0;$i $sourceAry[] = $url . $i;
}

foreach ($sourceAry as $k=>$v){
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,"http://dwz.cn/create.php");
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$data=array('url'=> $v);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
$strRes=curl_exec($ch);
curl_close($ch);
$arrResponse=json_decode($strRes,true);
if($arrResponse['status']==0){
/**错误处理*/
echo iconv('UTF-8','GBK',$arrResponse['err_msg'])."\n";
}
$destArray[] = $arrResponse['tinyurl'];
}
//print_r($destArray);
foreach ($destArray as $k=>$v){
echo $v . "";
}

修改$num就可以

相关标签: 批量生成url