502 Bad Gateway Foreach循环

时间:2017-07-21 15:26:07

标签: php laravel laravel-5.2

希望得到一些帮助,基本上我有一个脚本可以获取来自facebook用户的最新帖子,基本上我检查是否有任何新帖子在我的数据库中不可用,如果这篇文章是新的,比我将它与post id一起保存在我的数据库中(这样我检查DB上是否存在)。

但我有一个问题,在我的情况下,我需要检查一些用户,这个用户不断增长,在我的情况下,我有400个用户。如果我去100个用户,我当然得到500错误,这是很多请求。

那么有人有一个想法,我怎么能处理它?<​​/ p>

我的代码:FarcebookParcer.php

public function facebook($id, $num) {
    //Set your App ID and App Secret.
    $appID       = 'xxxxxxxxxx';
    $appSecret   = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx';
    //Create an access token using the APP ID and APP Secret.
    $accessToken = $appID . '|' . $appSecret;
    //Tie it all together to construct the URL
    $url         = "https://graph.facebook.com/$id/posts?fields=attachments,created_time&limit=$num&access_token=$accessToken";
    if (Helper::get_http_response_code($url) != 200) {
        return false;
    }
    //Make the API call
    $opts    = array(
        'http' => array(
            'method' => 'GET',
            'timeout' => 120
        )
    );
    $context = stream_context_create($opts);
    $result  = file_get_contents($url, false, $context);
    //Decode the JSON result.
    $dt      = json_decode($result, true);
    $posts   = $dt;
    return $posts;
}

CronController.php

public function socialfacebook() {
    $facebook     = SocialSnap::all();
    $socialparser = new FacebookParser();
    $appID        = 'xxxxxxxxxxx';
    $appSecret    = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
    $accessToken  = $appID . '|' . $appSecret;
    set_time_limit(0);
    foreach ($facebook as $key => $value) {
        if ($value->facebook != NULL) {
            $facebook_user  = $socialparser->facebook_user(substr($value->facebook, 1));
            $facebook_posts = $socialparser->facebook(substr($value->facebook, 1), 1);
            //Check if valid url
            if ($facebook_posts == false || $facebook_user == false) {
                continue;
            }
            if (isset($facebook_posts['data'][0]['attachments']['data'][0]['target']['url']) && isset($facebook_posts['data'][0]['attachments']['data'][0]['description'])) {
                SnapChat::where('facebook', $value->facebook)->update(['facebook_photo' => 'https://graph.facebook.com/'. substr($value->facebook, 1) . '/picture/?type=normal']);
                $post_current = SocialSnap::where('id_social', $facebook_posts['data'][0]['id'])->first();
                //return $post_current;
                if ($post_current == NULL) {
                    $post               = new SocialSnap;
                    $post->id_social    = $facebook_posts['data'][0]['id'];
                    $post->id_snapchats = $value->id;
                    $post->date_social  = isset($facebook_posts['data'][0]['created_time']) ? date("Y-m-d H:m:s", strtotime($facebook_posts['data'][0]['created_time'])) : "";
                    $post->type         = 'facebook';
                    $post->url          = $facebook_posts['data'][0]['attachments']['data'][0]['target']['url'];
                    $post->message      = $facebook_posts['data'][0]['attachments']['data'][0]['description'];
                    if (isset($facebook_posts['data'][0]['attachments']['data'][0]['media']['image']['src'])) {
                        $post->image = $facebook_posts['data'][0]['attachments']['data'][0]['media']['image']['src'];
                    }
                    $post->save();
                }
            }
        }
    }
    echo 'DONE';
}

0 个答案:

没有答案