下面是百度短網址介面產生代碼
$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 < $num; $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就可以