PHP内部的调用函数直到条件遇到

时间:2013-05-11 16:39:56

标签: php google-plus

我有一个我正在创建的功能,它基本上连接到Google Plus API和Pulls信息。不幸的是,为了一次获得多个结果,您需要使用API​​回调中提供的nextPageToken。以下是我到目前为止的情况:

function get_data($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
function parseResults($nextPageToken = "") {
    $count = 0;
    $content = get_data('https://www.googleapis.com/plus/v1/activities?query=%23throughglass&maxResults=10&orderBy=recent&fields=items(actor(displayName%2Curl)%2Cid%2Cobject(actor%2Cattachments)%2Curl%2Cverb)%2CnextPageToken&pageToken=' . $nextPageToken . '&key=YOUR API KEY HERE');
    $posts = json_decode($content);
    $token = $posts->nextPageToken;
    foreach ($posts->items as $value){
        $id = $value->id;
        $id_query = mysql_query("SELECT id FROM images WHERE id = '$id'");
        if ($value->verb != "post") continue;
        if (mysql_num_rows($id_query) > 0) continue;
        echo $value->actor->displayName . "<br />";
        $count++;
    }
    if ($count < 20){
        parseResults($token);
    }else{
        break;
    }

}
parseResults();

如果你能看到我想要做的事,希望有人可以帮助我。我基本上想继续使用提供的nextPageToken重新调用parseResults()函数,直到处理完20条记录。

2 个答案:

答案 0 :(得分:1)

我能够在劳伦斯的帮助下解决它:

function get_data($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}


function parseResults($nextPageToken = "", $count) {
    $content = get_data('https://www.googleapis.com/plus/v1/activities?query=%23throughglass&maxResults=10&orderBy=recent&fields=items(actor(displayName%2Curl)%2Cid%2Cobject(actor%2Cattachments)%2Curl%2Cverb)%2CnextPageToken&pageToken=' . $nextPageToken . '&key={YOUR_API_KEY}');
    $posts = json_decode($content);
    $token = $posts->nextPageToken;
    foreach ($posts->items as $value){
        $id = $value->id;
        $name = $value->actor->displayName;
        $profile = $value->actor->url;
        $post = $value->url;
        $post_text = $value->object->attachments[0]->displayName;
        $image_small = $value->object->attachments[0]->image->url;
        $image_full = $value->object->attachments[0]->fullImage->url;

        $id_query = mysql_query("SELECT id FROM images WHERE id = '$id'");
        if (mysql_num_rows($id_query) > 0) continue;

        if ($value->verb != "post") continue;
        if ($value->object->attachments[0]->image->url == "" || $value->object->attachments[0]->fullImage->url == "") continue;
        if ($post_text != "#throughglass") continue;

        mysql_query("INSERT INTO images (id, 
                                        author_name, 
                                        author_url, 
                                        post_url, 
                                        post_text, 
                                        image_small, 
                                        image_full) VALUES (
                                        '$id', 
                                        '$name', 
                                        '$profile', 
                                        '$post', 
                                        '$post_text', 
                                        '$image_small', 
                                        '$image_full')");
        echo "<b>ID: </b>" . $id . "<br />";
        echo "<b>Name: </b>" . $value->actor->displayName . "<br />";
        echo "<b>Profile URL: </b>" . $value->actor->url . "<br />";
        echo "<b>Post URL: </b>" . $value->url . "<br />";
        echo "<b>Post Text: </b>" . $value->object->attachments[0]->displayName . "<br />";
        echo "<b>Image Small: </b>" . $value->object->attachments[0]->image->url . "<br />";
        echo "<b>Image Full: </b>" . $value->object->attachments[0]->fullImage->url . "<br /><br />";
        $count++;
    }
    if ($count < 100){
        parseResults($token, $count);
    }else{
        echo "<br /><br /><br />" . $token;
        break;
    }

}
parseResults("", 0);

答案 1 :(得分:0)

我想你想计算递归调用的次数。

您可以通过将函数包装到类中并使用类属性来实现此目的,但最简单的方法是向函数parseResult添加参数并在下次调用之前递增它。