while循环中不支持循环使用curl的实例代码

发布时间:2020-06-18编辑:脚本学堂
本文分享一个php实例,看看while循环中不支持循环curl的处理方法,有需要的朋友参考下。

代码:

<?php
//while循环不支持循环curl
//by www.jb200.com
$link = mysql_connect('localhost', 'sms', 'sms');
mysql_select_db('sms', $link);
mysql_query("set names utf8");
$sql = "
SELECT phone,chang, msg, linkid, mo_time FROM tables '";

$result = mysql_query($sql,$link);
$array = array();
while($row = mysql_fetch_array($result)){
    $linkid = $row['phone'].date("YmdHis", strtotime($row['mo_time']));
    $str = "SPNUM=".$row['chang']."&MOBILE=".$row['phone']."&CONTENT=".urlencode($row['msg'])."&MOTIME=".urlencode($row['mo_time']).
"&LINKID=".$linkid;

    $url = "www.jb200.com?".$str; 
    $array[] = $url;
    //var_dump($url); 这里使用curl访问,只能访问一条随后就中断了,只能在上面存放到数组中
    /*$result = file_get_contents($url);
    var_dump($result);*/
    /*$ch = curl_init($url);
    curl_setopt($ch, CURLOPT_TIMEOUT, 40);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
     // 4. 释放curl句柄
      curl_close($ch);*/
}
//var_dump($array); 
//这里使用foreach循环执行curl命令
foreach ($array as $url) {
    var_dump($url);
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_TIMEOUT, 40);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
     // 4. 释放curl句柄
      curl_close($ch);
      var_dump($result);
}
?>