doumao6048 2017-09-08 21:20
浏览 81
已采纳

在后台调用PHP循环中的PHP文件

I have a PHP loop where i need to call another PHP file in the background to insert/update some information based on a variable send to it. I have tried to use CURL, but it does not seem to work.

I need it to call SQLupdate.php?symbol=$symbol - Is there another way of calling that PHP with the paramter in the background - and can it eventually be done Synchronously with a response back for each loop?

while(($row=mysqli_fetch_array($res)) and ($counter < $max))
{
$ch = curl_init();
$curlConfig = array(
    CURLOPT_URL            => "SQLinsert.php",
    CURLOPT_POST           => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS     => array(
        'symbol' => $symbol,

    )
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
}
  • 写回答

1条回答 默认 最新

  • doure5236 2017-09-11 18:42
    关注

    I'm going to weigh in down here in hopes of getting this one "away & done".

    Although it isn't entirely clear from your post, it seems you're trying to call your PHP file via an HTTP(s) protocol.

    In many configurations of PHP, you could do this and avoid some potential cURL overhead by using file_get_contents() instead:

    while(($row=mysqli_fetch_array($res)) and ($counter < $max)) {
    
        $postdata = http_build_query(
            array(
                'symbol' => $row['symbol']
            )
        );
    
        $opts = array('http' =>
            array(
                'method'  => 'POST',
                'header'  => 'Content-type: application/x-www-form-urlencoded',
                'content' => $postdata
            )
        );
    
        $context = stream_context_create($opts);
    
        $result = file_get_contents('http://example.com/SQLinsert.php', false, $context);
    
        $counter++; // you didn't mention this, but you don't want a everloop...
    }
    

    That's pretty much a textbook example copied from the manual, actually.

    To use cURL instead, as you tried to do originally, and in truth it seems pretty clean with one call to curl_setopt() inside the loop:

    $ch = curl_init();
    $curlConfig = array(
        CURLOPT_URL            => "http://example.com/SQLinsert.php",
        CURLOPT_POST           => true,
        CURLOPT_RETURNTRANSFER => true
    );
    curl_setopt_array($ch, $curlConfig);
    
    while(($row=mysqli_fetch_array($res)) and ($counter < $max)) {
    
        curl_setopt($ch, CURLOPT_POSTFIELDS, array('symbol' => $row['symbol']));
        $result = curl_exec($ch);
        $counter++; //see above
    }
    // do this *after* the loop
    curl_close($ch);
    

    Now the actual and original problem may be that $symbol isn't initialized; at least, it isn't in the example you have provided. I've attempted to fix this by using $row['symbol'] in both my examples. If this isn't the name of the column in the database then you would obviously need to use the correct name.

    Finally, be advised that it's almost always better to access a secondary resource via the fastest available mechanism; if "SQLinsert.php" is local to the calling script, using HTTP(s) is going to be terribly under-performant, and you should rewrite both pieces of the system to work from a local (e.g. 'disk-based') point-of-view (which has already been recommended by a plethora of commenters):

    //SQLinsert.php
    function myInsert($symbol) {
        // you've not given us any DB schema information ...
        global $db; //hack, *cough*
        $sql = "insert into `myTable` (symbol) values('$symbol')";
        $res = $this->db->query($sql);
        if ($res) return true;
        return false;
    }
    
    //script.php
    
    require_once("SQLinsert.php");
    
    while(($row=mysqli_fetch_array($res)) and ($counter < $max)) {
    
        $ins = myInsert($row['symbol']);
    
        if ($ins) { // let's only count *good* inserts, which is possible
                   // because we've written 'myInsert' to return a boolean
            $counter++;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率