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 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭