Batch generate URLs the following code is generated by Baidu short url interface
$ 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) {/** error handling */echo iconv ('utf-8', 'gbk', $ arrResponse ['err _ msg ']). "\ n";}/** tinyurl */echo $ arrResponse ['tinyurl']. "\ n ";
In this way, only one output can be generated.
Because I want to generate multiple
Www.xxx.cn /? Post = 1
Www.xxx.cn /? Post = 2
Www.xxx.cn /? Post = 3
......
Www.xxx.cn /? Posting = 100
Www.xxx.cn /? Posting = 101
The sequential link is converted into a short URL.
Reply to discussion (solution)
Rewrite your code as a function and pass the data array as a parameter
No program. can you help solve this problem? thank you.
$ 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 ){
/** Handle errors */
Echo iconv ('utf-8', 'gbk', $ arrResponse ['err _ msg ']). "\ n ";
}
$ DestArray [] = $ arrResponse ['tinyurl'];
}
// Print_r ($ destArray );
Foreach ($ destArray as $ k => $ v ){
Echo $ v ."
";
}
Modify $ num.