【问题标题】:running curl in a for loop blocks access to website在 for 循环中运行 curl 会阻止访问网站
【发布时间】:2022-12-04 23:49:54
【问题描述】:

我有以下代码,它基本上在 for 循环中运行一堆 curl 请求 这是一个后台系统作业..但是当此代码运行时..我无法访问该网站,网站正在加载直到此任务/功能完成

public function endcount() {

        $order_list = $this->order_model->get_rows([
            'where' => [
              "status IN ('Processing', 'In Progress') AND service_id = 1"
            ],
            'order_by' => 'quantity ASC',
            'limit' => '200'
        ]);

    
        
        foreach ($order_list as $key => $value) {

            $start_count = $value['start_count'];
            
            
            //service id = 1 is for followers
            if ($value['service_id'] == '1' || $value['service_id'] == '3' || $value['service_id'] == '5') {
                
                

                $end_count = 0;

                
                try {
                    

            
                    $username = $value['target'];
                    $proxy = '139.99.54.49:10163';
                    $proxyauth = 'username:password';

                    $url = 'https://www.instagram.com/'. $username .'/?__a=1&__d=dis';
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL, $url);
                    curl_setopt($ch, CURLOPT_PROXY, $proxy);     // PROXY details with port
                    curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);   // Use if proxy have username and password
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
                    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json') );
                    $data = curl_exec($ch);
                    if (curl_errno($ch)) {
                        $error_msg = curl_error($ch);
                        var_dump($error_msg);
                    }

                    $json_data = json_decode($data, true);
                    if ($json_data && array_key_exists('graphql', $json_data)) {
                        $end_count = $json_data['graphql']['user']['edge_followed_by']['count'];
                    } else {
                        var_dump($username . ' data ga bisa');
                        $end_count = 0;
                    }
                    

                    curl_close($ch);


                } catch(Exception $e) {
                    var_dump($e->getMessage());
                    $end_count = 0;
                }

                if ($end_count == -666) {
                    $end_count = $start_count + 5;
                }
                

                $total_followers = $start_count + $value['quantity'];
                $remains = $total_followers - $end_count;

                if ($remains <= 10) {
                    $status = 'Success';
                } else {
                    $status = 'Partial';
                }

            
                var_dump('end count of ' . $value['target'] . ' ' . $end_count . ' WITH REMAINS: ' . $remains);

                if ($status == 'Success') {
                    $update_order = [
                    'remains' => $remains,
                    'status' => $status,
                    'updated_at' => date('Y-m-d H:i:s'),
                    ];
                    $update_order = $this->order_model->update($update_order, ['id' => $value['id']]);
                    if ($update_order == true) {
                        print('ID '.$value['id'].' => ['.$status.'] - [FINAL COUNT : '.$end_count.'] - [REMAINS : '.$remains.'] - [TARGET COUNT : '.$total_followers.']<br>');
                    } else {
                        print('Error..');
                    }
                }
                
                //var_dump($value['target']);
            } else if ($value['service_id'] == '2') {
                //service id = 2 is for likers

            }
            
        }
        
}

有什么方法可以优化此代码,使其在运行时不会阻止访问该站点?

【问题讨论】:

    标签: php php-5.6


    【解决方案1】:

    尝试添加睡眠(5);对您的代码起作用,这样服务器就不会因请求而超载..

    你的代码应该是这个样子:

     foreach ($order_list as $key => $value) {
    
            sleep(5);
    
            $start_count = $value['start_count'];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-24
      • 2021-02-01
      • 1970-01-01
      • 2011-06-27
      • 1970-01-01
      • 1970-01-01
      • 2019-01-13
      相关资源
      最近更新 更多